# When are attr\_reader type methods called?

**URL:** <https://www.ruby-forum.com/t/when-are-attr-reader-type-methods-called/50787>\
**Category:** Ruby\
**Created:** [December 21, 2005, 5:14am UTC](https://www.ruby-forum.com/t/when-are-attr-reader-type-methods-called/50787 "2005-12-21T05:14:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [December 21, 2005, 5:14am UTC](https://www.ruby-forum.com/t/when-are-attr-reader-type-methods-called/50787/1 "2005-12-21T05:14:09Z")

</div>

I couldnt get a proper subject for the post but here is my question.  
Its regarding methods like  
attr\_reader defined like below  
You can do this in ruby

# file a.rb

class A  
call\_this (“hello world”)

def call\_this  
…  
end  
end

The A::call\_this is called immediately after I run the script.Becuase I  
made the method print something and it printed it even though I hadnt  
instantiated an object of the class with new.  
What if the method uses some instance variables? wouldnt that be a  
problem because its called even before an object is created?My question  
is at what time during program execution are these methods called? If  
there are more than one classes in a file then whats the order ? is it  
defined by the language?  
My guess is that these are like the equivalent of static member  
variables in C, initialized before \_main is called.?  
Hope to get some clarification on this  
vivek

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [December 21, 2005, 5:38am UTC](https://www.ruby-forum.com/t/when-are-attr-reader-type-methods-called/50787/2 "2005-12-21T05:38:13Z")

</div>

On 12/20/05, Vivek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …  
> there are more than one classes in a file then whats the order ? is it  
> defined by the language?  
> My guess is that these are like the equivalent of static member  
> variables in C, initialized before \_main is called.?  
> Hope to get some clarification on this  
> vivek

Actually your code will generate an error. You can do this:

class Foo  
def self.bar  
puts 42  
end

bar  
end

This will work as the method bar exists and belongs to the class Foo,  
not an instance of Foo. This will not work:

class Boo  
def hoo  
puts “Python is nice, but I like Ruby better :-)”  
end

hoo # this will raise a NoMethodError, unless hoo had been  
defined further up the chain  
end

Of course you can work around much of this with fun method\_missing  
maddness. In the particular case of attr\_reader, they are actually  
part of Module which is the superclass of Class, that is why you can  
call them inside your class definitions – they are methods available  
in Class (and you are defining a Class).

pth

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [December 21, 2005, 6:14am UTC](https://www.ruby-forum.com/t/when-are-attr-reader-type-methods-called/50787/3 "2005-12-21T06:14:20Z")

</div>

On Wed, 21 Dec 2005 04:09:33 -0000, Vivek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> …  
> there are more than one classes in a file then whats the order ? is it  
> defined by the language?

It’s top to bottom, as you’d expect. Does the following surprise you?

NewClass = Class.new(Array) do

```
 attr_accessor :myattr

 def this
   "that"
 end

```

end

puts (nc = NewClass.new).this  
nc.myattr = 16  
puts nc.myattr

nc[3] = “Aha!”  
p nc

I just made a new nuby file trying out the attr\_accessor and instance  
var  
stuff you asked about:

```
http://roscopeco.co.uk/code/noob/class-def.html

```

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [December 22, 2005, 11:08am UTC](https://www.ruby-forum.com/t/when-are-attr-reader-type-methods-called/50787/4 "2005-12-22T11:08:06Z")

</div>

On 12/22/05, Vivek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> puts arg
> 
> ```
> 
> end  
> end  
> puts X.instance\_eval { @var}  
> puts X.instance\_eval { @ivar}
> 
> I have a statement like @var = 1 inside a class method ‘call\_this’  
> ,but @var is an instance variable,so who come I dont get an error?

You are asking the right questions, if you are feeling adventurous do a  
quick:  
ri Class

X is also an Object – the variable @ivar is an instance variable in  
that class. Similarly the instance variable @var you create in the  
call\_this method, becomes an instance variable in the class A. Try  
this:

puts A.instance\_eval { @var } # → 1

Also note that if you:

X.call\_this  
puts X.instance\_eval { @var } # → 1

> And second  
> I read the documentation of instance\_eval at The practical programmer  
> site…which says.
> 
> instance\_eval obj.instance\_eval(aString [, file [line] ] ) →   
> anObject
> 
> But here and in Ross’ examples we call it on a class ? Is the  
> documentation incorrect or am i missing something.

You were (because I know you got it from my pitiful explanation 🙂  
were missing that a Class is an Object.

Good Luck and keep at it  
pth

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [December 22, 2005, 10:10am UTC](https://www.ruby-forum.com/t/when-are-attr-reader-type-methods-called/50787/5 "2005-12-22T10:10:54Z")

</div>

Ok …sorry for the wrong code.What I meant was do have a class  
method.Thanks for the explanation …so now I have this below (I learnt  
the instance\_eval from Ross’ examples)

class X  
@ivar = 1  
def self.call\_this  
puts “call this”  
@var = 1  
end  
end  
class A \<X  
call\_this  
def some\_method(arg)  
puts arg  
end  
end  
puts X.instance\_eval { @var}  
puts X.instance\_eval { @ivar}

So I execute the above program and call\_this is called. So I guess  
these methods which are called in a class but outside any function have  
to be class methods and are called in a sequential order as they are  
encountered by the interpreter.  
This brings me to another question  
I have a statement like @var = 1 inside a class method ‘call\_this’  
,but @var is an instance variable,so who come I dont get an error?  
Although when I try to print it out it gives ‘nil’ .Is this some thing  
which ruby forgives ?

And second  
I read the documentation of instance\_eval at The practical programmer  
site…which says.

instance\_eval obj.instance\_eval(aString [, file [line] ] ) -\>  
anObject

But here and in Ross’ examples we call it on a class ? Is the  
documentation incorrect or am i missing something.

thanks again  
vivek

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [December 22, 2005, 2:29pm UTC](https://www.ruby-forum.com/t/when-are-attr-reader-type-methods-called/50787/6 "2005-12-22T14:29:40Z")

</div>

On Thu, 22 Dec 2005 10:05:44 -0000, Patrick H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On 12/22/05, Vivek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > But here and in Ross’ examples we call it on a class ? Is the  
> > documentation incorrect or am i missing something.
> 
> You were (because I know you got it from my pitiful explanation 🙂  
> were missing that a Class is an Object.

(OP:) See also my ‘Eureka’ moment:  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/167586](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/167586) .

I have a feeling you’re not far from discovering Ruby magic - stick  
with  
it 🙂
