Forum: Ruby attr_reader explained

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Marc n. (Guest)
on 2007-03-03 19:55
(Received via mailing list)
can someone explain how attr_reader works?

i can't find a good explanation anywhere.

please help!
Tim B. (Guest)
on 2007-03-03 20:15
(Received via mailing list)
On 3/3/07, libsfan01 <removed_email_address@domain.invalid> 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 S. (Guest)
on 2007-03-03 20:21
(Received via mailing list)
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 C. (Guest)
on 2007-03-03 21:47
(Received via mailing list)
Alle sabato 3 marzo 2007, Raj S. 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 Z. (Guest)
on 2007-03-04 01:27
(Received via mailing list)
On 3/3/07, Tim B. <removed_email_address@domain.invalid> wrote:
> On 3/3/07, libsfan01 <removed_email_address@domain.invalid> 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
Eric H. (Guest)
on 2007-03-05 23:30
(Received via mailing list)
On Mar 3, 2007, at 15:25, Austin Z. wrote:
> 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-...
define_method
This topic is locked and can not be replied to.