Attr Methods and object setters

G

Gregory Brown

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
 
G

Gregory Brown

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

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

Ooh, neat trick. Thanks for sharing.

-greg
 
R

Rick DeNatale

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

=A0Nothing =3D Object.new
=A0def name(value =3D Nothing)
=A0 =A0@name =3D value unless value =3D=3D Nothing
=A0end

Actually the second line IS necessary

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

name 42 # =3D> 42
name # =3D> nil



--=20
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
 
F

Fabian Streitel

[Note: parts of this message were removed to make it a legal post.]
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
 
D

David Masover

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
 
B

Brian Candler

David said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top