Hi Marconi,
The "trick" is a change in perspective. In ruby, everything is a
function. Getters & setters, though they look like object properties,
are methods.
class Foo
attr_reader :read_only # this is like: def read_only; @read_only; end
attr_writer :write_only # this is like: def write_only=(x);
@write_only=x; end
attr_accessor :read_write # this is like def read_write;
@read_write; end; def read_write=(x); @read_write=x; end
def wombat
'This is as good as a "constant" property.'
end
end
Setters are kind of special syntactically in that you can do
foo.read_write = 'hello world' # foo.read_write=('hello world')
A more abstract perspective is by thinking that you are "sending a
message" instead of "calling a function"
foo.read_only # send message named 'read_only' to foo
foo.read_write = 1 # send message named 'read_write=' to foo, with parameter 1
- Jon