Attr Methods and object setters

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.

-greg

On Sat, Jun 27, 2009 at 11:26 PM, Daniel DeLorme[email protected]
wrote:

Another way to do this is to use some kind of singleton object

Nothing = Object.new
def name(value = Nothing)
@name = value unless value == Nothing
@name
end

Ooh, neat trick. Thanks for sharing.

-greg

Hi –

On Sun, 28 Jun 2009, Gregory B. wrote:

Another way to do this is to use some kind of singleton object

Nothing = Object.new
def name(value = Nothing)
 @name = value unless value == Nothing
 @name
end

At the risk of someone saying that I’ve made it too cryptic, it could
be one line shorter:

Nothing = Object.new
def name(value = Nothing)
@name = value unless value == Nothing
end

David

On Sun, Jun 28, 2009 at 10:18 AM, David A. Black[email protected]
wrote:

At the risk of someone saying that I’ve made it too cryptic, it could
be one line shorter:

Nothing = Object.new
def name(value = Nothing)
@name = value unless value == Nothing
end

Actually the second line IS necessary

Nothing = Object.new
def name(value = Nothing)
@name = value unless value == Nothing
end

name 42 # => 42
name # => nil


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

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.

alright then! my bad…
I thought you meant this to be generally an alternative to the
traditional
setters.

Greetz!
k

2009/6/28 Gregory B. [email protected]

Hi –

On Sun, 28 Jun 2009, Rick DeNatale wrote:

Actually the second line IS necessary

Nothing = Object.new
def name(value = Nothing)
@name = value unless value == Nothing
end

name 42 # => 42
name # => nil

Whoops – right you are. Rewind.

(At least you didn’t say it was cryptic :slight_smile:

David

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

David M. wrote:

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

I believe that “same object” is slightly better expressed by:

Nothing.equal? value

although in this case, for an instance of Object, it should be the same.