Why cool guys use obj.instance_eval instead of class << obj to define singletons?
Victor Moroz
5 posts
|
Or why one should use class << obj if you have instance_eval? After all instance_eval is more convenient and is executed in the ‘obj’ scope (e.g. you can use instance methods), while class << obj is something odd as it is executed in the scope of the ghost class. What can be done in this scope except of defining singletons? Well, just about my favorite
This means there is not ghost class between singleton ghost class and Class while there is between String and Class for example. Confusing… |
Victor Moroz
5 posts
|
And even more confusing here
So when we execute instance_eval and class_eval both blocks are executed in the context of String class (self = String), but ‘def method’ does different things! Well, concept ‘current class’ is reasonable, but how far this concept goes? It means there are two contexts – ‘self’ for calling methods and accessing variables and ‘current class’ for defining methods? Where else these contexts are different? |
Dave Thomas
Administrator
70 posts
|
As to your first point: that’s one reason I think For the second point: the biggest place the two are different is at the top level of your program… |
Victor Moroz
5 posts
|
Actually Wow, I just realized that top level works as Object.class_eval block! |
Dave Thomas
Administrator
70 posts
|
Victor: I know people use it. I just think they shouldn’t… :) |
5 posts, 2 voices
