I play with metaprogramming in ruby and I have a question. I have a class:
class Klass
class << self
@x = "yeah"
end
end
b = Klass.new
a = class << Klass; self; end
a.instance_eval "@x" #=> yeah
Klass.instance_eval "@x" #=> nil
Why? In variable a I have a singleton class, right? And Klass.instance_eval exec in context of a singleton class:
Klass.instance_eval "def yeah; puts 10; end"
Klass.yeah #=> 10
Also, Klass in interpreter points to context of class, yes? And a points to context of a singleton class?
And which indicates a.class_eval and a.instance_eval? I do:
a.instance_eval "def pops; puts 0; end"
a.class_eval "def popsx; puts 1; end"
a.pops #=> 0
a.popsx # FAIL
Klass.pops # FAIL
Klass.popsx #=> 1
b.pops; b.popsx # DOUBLE FAIL
and I do not understand this. Thanks!
First, while it seems like eigentclass is used by some people singleton class is more common term. Singleton class contains object-specific behavior for an object in Ruby. You can’t create other instances of that class except the original object this singleton class belongs to.
Speaking about defining of methods inside different types of eval this article introduces nice rule for methods defined in
instance_evalandclass_eval:That pretty much describes the situation.
There was a huge write-up about classes that are instances of Class class, their singleton classes that are subclasses of Class class and some other crazy stuff (not that much related to the problem). But as your question can be easily applied to regular objects and their classes (and it makes things much easier to explain), I decided to remove that all (though, you can still see that stuff in revisions history of the answer).
Let’s look at regular class and instance of that class and see how that all works:
Method definitions inside different types of eval:
Defining object-specific methods:
Let’s now look at singleton class of object
a:Now let’s look at instance variables:
And now to instance variables inside
class_eval:Now if you replace class
Awith classClassand objectawith objectKlass(that in this particular situation is nothing more than instance of classClass) I hope you’ll get explanation to your questions. If you still have some feel free to ask.