object.@attribute

R

rolo

Why cannot we have attribute access of the object from anywhere in the program by sennding a message @attribute

class Example
def initialize
@number = '1.0'
end
end

e = Example.new
print e.@number => should print 1.0


rolo
 
H

Hal Fulton

rolo said:
Why cannot we have attribute access of the object from anywhere in the program by sennding a message @attribute

class Example
def initialize
@number = '1.0'
end
end

e = Example.new
print e.@number => should print 1.0

That's not how it works.

In Ruby, all data members are private by default. They can be
accessed *only* through methods.

I don't recognize your name offhand. If you are a nuby, you may
not be aware of these possibilities for the class:

attr_reader :number # Let outside world read this value
attr_writer :number # " " " set it
attr_accessor :number # " " " read or set it

In practice, I've never used attr_writer. It's hard to imagine
something useful being write-only and not readable. (Insert
Perl wisecrack here.)


Hal
 
S

Shu-yu Guo

Hal said:
In practice, I've never used attr_writer. It's hard to imagine
something useful being write-only and not readable. (Insert
Perl wisecrack here.)

I use it when I want to define a toggle, like the following:

class Foo
attr_writer :bar
def bar?
@bar
end
end

Not that I _can't_ use attr_accessor, I just don't want to in this case.
 
T

Tim Hunter

In practice, I've never used attr_writer. It's hard to imagine something
useful being write-only and not readable. (Insert Perl wisecrack here.)

Well, I'll leave it up to you to decide whether or not it's useful, but
RMagick defines special classes whose purpose is to collect optional
arguments when a method has a _lot_ of optional arguments. For example,
the montage method has 15 optional arguments, so it creates a "montage
arguments" object and yields in that object's context:

img.montage {
self.background_color = 'red'
self.font = 'Arial'
# ... etc.
}

These attributes are write-only.

I'm not claiming this was my idea, but offhand I forget where I saw it :)
 
J

Jason Voegele

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

attr_reader :number # Let outside world read this value
attr_writer :number # " " " set it
attr_accessor :number # " " " read or set it

In practice, I've never used attr_writer. It's hard to imagine
something useful being write-only and not readable. (Insert
Perl wisecrack here.)

Hehe...I'll avoid the Perl wisecrack, but I can think of a couple scenarios
where attr_writer might be useful:

1) True write-only data, such as a password. For example, you might have
password=(pwd) to set the password, and verify_password(pwd) to compare the
password to a given string, but no password reader method. This is
especially useful if you store passwords in hashed form so that they cannot
be retrieved once hashed.

2) You might want to use a standard attr_writer generated writer method, but
provide a custom reader method. I do this all of the time with boolean
attributes, so I can add the '?' to the end of the reader method.

- --
Jason Voegele
Sodd's Second Law:
Sooner or later, the worst possible set of circumstances is
bound to occur.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFAcp/1rFeZdK3QLzERAhgNAJ0ZZpDvz5OnItwHVSaA0z7MuKAnTwCfe7qX
xVw5mhB/UXbzZsVmrbLeTmg=
=xxYw
-----END PGP SIGNATURE-----
 
G

George Ogata

Jason Voegele said:
2) You might want to use a standard attr_writer generated writer method, but
provide a custom reader method. I do this all of the time with boolean
attributes, so I can add the '?' to the end of the reader method.

I've hit this before too. Maybe attr_accessor:)foo?) should define
'foo?' and 'foo=' (instead of the current 'foo?' and 'foo?=')?

Or maybe this syntax should be allowed:

obj.foo? = true

Hmm?
 
H

Hal Fulton

George said:
I've hit this before too. Maybe attr_accessor:)foo?) should define
'foo?' and 'foo=' (instead of the current 'foo?' and 'foo?=')?

Or maybe this syntax should be allowed:

obj.foo? = true

Hmm?

FWIW, I've considered an attr_query that created foo= and foo? methods.

Yesterday I also considered attr_proc that would construct methods like
this:

obj.doit = proc { do_stuff } # set the callback
obj.doit # call it!

Of course, this sort of thing is easily doable in Ruby, so I don't see
a core change as appropriate unless this became widespread.


Hal
 

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

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top