[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

attr_reader explained

libsfan01

3/3/2007 5:50:00 PM

can someone explain how attr_reader works?

i can't find a good explanation anywhere.

please help!

5 Answers

Tim Becker

3/3/2007 6:15:00 PM

0

On 3/3/07, libsfan01 <mcyi2mr3@googlemail.com> wrote:
> can someone explain how attr_reader works?
>
> i can't find a good explanation anywhere.
>
> please help!


It's built in to the language, so it 'works' by magic for all
practical purposes, it's implemented in `object.c` if you want to take
a look at the implementation. If you wanted to implement it in Ruby,
you do something like this:

> def new_attr_reader cl, sym
> str = "def #{sym.to_s}; @#{sym.to_s}; end"
> cl.class_eval str
> end

>class Test; end
>new_attr_reader Test, :my_new_reader
>Test.new().my_new_reader

The built in `attr_reader` takes an array of sym's, but you get the idea.
-tim

Raj Sahae

3/3/2007 6:21:00 PM

0

libsfan01 wrote:
> can someone explain how attr_reader works?
>
> i can't find a good explanation anywhere.
>
> please help!
>
attr_reader, in it's common usage, creates instance variables and
defines a method by which you can read them.

Class Test
def initialize(num)
@test=num
end

def test
@test
end
end

If you were to use attr_reader instead, your class definition would be

Class Test
attr_reader :test
def initialize(num)
@test = num
end
end

Stefano Crocco

3/3/2007 7:48:00 PM

0

Alle sabato 3 marzo 2007, Raj Sahae ha scritto:
> attr_reader, in it's common usage, creates instance variables and
> defines a method by which you can read them.

Actually, attr_reader doesn't create the variable, just the accessor method.
To see this, do the following in irb:

> class C
> attr_reader :var
> def var_defined?
> defined?(@var)
> end
> end
=> nil
> c=C.new
=> #<C:0xb7a4c650>
> c.var_defined?
=> nil
> class C
> def initialize
> @var=nil
> end
> end
=> nil
> c1=C.new
=> #<C:0xb7cf1a04 @var=nil>
> c1.var_defined?
=> "instance-variable"

As you can see, in the first case (the variable called c) doesn't have the
@var instance variable defined. Even after you call the var method defined
using attr_reader, the variable is still not defined. To define it, you need
to explicitly assign it to a value (using @var= something inside an instance
method of the class or using instance_variable_set)

Stefano

Austin Ziegler

3/3/2007 11:26:00 PM

0

On 3/3/07, Tim Becker <a2800276@gmail.com> wrote:
> On 3/3/07, libsfan01 <mcyi2mr3@googlemail.com> wrote:
> > can someone explain how attr_reader works?
> > i can't find a good explanation anywhere.
> It's built in to the language, so it 'works' by magic for all
> practical purposes, it's implemented in `object.c` if you want to take
> a look at the implementation. If you wanted to implement it in Ruby,
> you do something like this:

Actually, that's not true. It's written in C, but that's not the same
as being built into the language. It's a method on Module, so it *can*
be overridden.

>> class Module
>> alias old_attr_reader attr_reader
>> def attr_reader(*names)
>> puts "Making attribute readers for #{names.join(", ")}"
>> old_attr_reader *names
>> end
>> end
=> nil
>> class Foo
>> attr_reader :bar
>> attr_reader :baz, :quux
>> end
Making attribute readers for bar
Making attribute readers for baz, quux

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Eric Hodel

3/5/2007 9:29:00 PM

0

On Mar 3, 2007, at 15:25, Austin Ziegler wrote:
> On 3/3/07, Tim Becker <a2800276@gmail.com> wrote:
>> On 3/3/07, libsfan01 <mcyi2mr3@googlemail.com> wrote:
>> > can someone explain how attr_reader works?
>> > i can't find a good explanation anywhere.
>> It's built in to the language, so it 'works' by magic for all
>> practical purposes, it's implemented in `object.c` if you want to
>> take
>> a look at the implementation. If you wanted to implement it in Ruby,
>> you do something like this:
>
> Actually, that's not true. It's written in C, but that's not the same
> as being built into the language. It's a method on Module, so it *can*
> be overridden.

attr* also cheats, so methods defined by attr* are slightly faster
than methods defined with def:

http://blog.segment7.net/articles/2006/03/06/attr-vs-...
define_method