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
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