I’m having some confusion with the use of
attr_reader/writer/accessor,etc.
Could someone point me to a document that explains clearly, and in
depth?
Check the Pickaxe p. 24, or online
http://www.rubycentral.com/book/tut_classes.html, under the heading
“Objects and Attributes”.
There’s not really much to say. The attr_reader method creates an
instance variable and a method that returns its value. The attr_writer
method creates an instance variable and a method that sets its value. The
attr_accessor method does both.
class MyClass
attr_reader :foo
end
is shorthand for
class MyClass
def foo
@foo
end
end
Similarly,
class MyClass
attr_writer :foo
end
is shorthand for
class MyClass
def foo=(f)
@foo=f
end
end
and finally
class MyClass
attr_accessor :foo
end
is shorthand for
class MyClass
def foo
@foo
end
def foo=(f)
@foo=f
end
end
···
On Mon, 23 Dec 2002 05:13:35 +0900, gtzi wrote:
I’m having some confusion with the use of
attr_reader/writer/accessor,etc.
Could someone point me to a document that explains clearly, and in
depth?
Thank you.
And, i apologize for sending an attachment in my email.
I forgot to de-select the signature before i sent my message.
···
On Sunday, December 22, 2002, at 03:20 PM, Tim Hunter wrote:
Check the Pickaxe p. 24, or online
http://www.rubycentral.com/book/tut_classes.html, under the heading
“Objects and Attributes”.There’s not really much to say. The attr_reader method creates an
instance variable and a method that returns its value. The attr_writer
method creates an instance variable and a method that sets its value.
The
attr_accessor method does both.class MyClass
attr_reader :foo
endis shorthand for
class MyClass
def foo
@foo
end
endSimilarly,
class MyClass
attr_writer :foo
endis shorthand for
class MyClass
def foo=(f)
@foo=f
end
endand finally
class MyClass
attr_accessor :foo
endis shorthand for
class MyClass
def foo
@foo
end
def foo=(f)
@foo=f
end
endOn Mon, 23 Dec 2002 05:13:35 +0900, gtzi wrote:
I’m having some confusion with the use of
attr_reader/writer/accessor,etc.
Could someone point me to a document that explains clearly, and in
depth?