instance_eval trickiness

7

7stud --

Hi,

I think instance_eval() is the trickiest of the eval's to understand.
"Metaprogramming Ruby" says there are two concepts that you have to be
aware of:

1) which object is self
2) which object is the current class

Unlike self, there is no keyword that you can print out that tells you
what the current class is. The most obvious indicator of what the
current class is in your code is the 'class' keyword:

class Dog
...
end

Between 'class' and 'end' the current class is Dog. At times it is
important to know what the current class is because def's attach
themselves to the current class. In the case of classes like Dog,
inside the Dog class (but outside of any method definitions)it so
happens that self and the current class are the same:

class Dog
#in here the current class is Dog

puts self #=> Dog
end
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top