Ruby's def and instance_eval vs. class_eval?

I think your confusion comes from the fact that def does not depend on the current self, you might think about it as being a "current class" that has it's own rules Following your examples: class A # defs here go to A puts self # => A class However, they differ in the way they set up the environment for method definition. Class_eval sets things up as if you were in the body of a class definition, so method definitions will define instance methods In contrast, calling instance_eval on a class acts as if you were working inside the singleton class of self. Therefore, any methods you define will become class methods The only thing I think is worth adding is taht you can call instance_eval in any object, not just classes, and the behaviour doesn't change but has different consequences Some relevant reading: Ruby: instance_eval and class_eval method definitions Chapter 4 of this most excelent series.

I think your confusion comes from the fact that def does not depend on the current self, you might think about it as being a "current class" that has it's own rules. Following your examples: class A # defs here go to A puts self # => A class Instance_eval do #defs here go to A's eigenclass end s = " World" class Therefore, any methods you define will become class methods. The only thing I think is worth adding is taht you can call instance_eval in any object, not just classes, and the behaviour doesn't change but has different consequences.

Some relevant reading: Ruby: instance_eval and class_eval method definitions Chapter 4 of this most excelent series.

Hm, I see -- that makes sense. Is there any way to inspect that "current class" in Ruby code? – Jo Liss Dec 10 '10 at 13:25 1 I think in A.

Instance_eval, you meant to write #defs here go to A's eigenclass? Or am I mistaken? – Jo Liss Dec 10 '10 at 13:33 It's not exactly simple, I will post some relevant links in the answer – URL1 Dec 10 '10 at 13:36 @Jo Liss, of course, thank you – krusty.

Ar Dec 10 '10 at 13:42 @krusty. Ar: Google for singleton class, meta class, or eigenclass.(I have seen it referred to as all 3) It is an instance specific class that sits between the instance and the instances class in the inheritance heirarchy – Matt Briggs Dec 10 '10 at 13:52.

Just to add to @krusty. Ar's answer: def and define_method add methods to the current method definition context (I believe that's what it's called, I'm not sure), not to the current self. It's just that inside of a module, class or singleton class body, those two happen to be the same.

But, for example, in a script body (aka top-level), self is the main object, but the current method definition context is Object.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions