On Sun, Jun 28, 2009 at 9:01 AM, Fabian
Streitel[email protected] wrote:
But for any normal class that does not provide DSL semantics, I’d still go
with normal
getters and setters. I just don’t see the point of reducing the amount of my
typing by
a single =, when on the other hand I have to either construct a whole array
each time
or introduce a new neutral element.
To mee that just doesn’t feel right, I guess…
Oh, I’m not sure if we crossed threads at some point, but I was
suggesting using that approach only for DSLs.
So we are totally in agreement.
On Saturday 27 June 2009 10:26:51 pm Daniel DeLorme wrote:
Nothing = Object.new
def name(value = Nothing) @name = value unless value == Nothing @name
end
Ok, I guess you have to be trying, but I can still break this:
class Everything
def == other
true
end
end
name Everything.new
And the way to fix it:
Nothing = Object.new
def name(value = Nothing) @name = value unless Nothing == value @name
end
Out of sheer curiosity, I did benchmark this, and the Nothing method is
slightly faster. Probably worth it if anyone wants to put this in a
library –
otherwise, I’ll go with the more readable (to me):
def name(*args) @name = args.first unless args.empty? @name
end