When are attr_reader type methods called?

V

Vivek

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
 
P

Patrick Hurley

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

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
 
V

Vivek

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
 
P

Patrick Hurley

class X
@ivar =3D 1
def self.call_this
puts "call this"
@var =3D 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}


I have a statement like @var =3D 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 qui=
ck:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top