Bharat Ruparel wrote: > Going through the Ruby Programming Language text Chapter 8 Reflection > and MetaProgramming. Section 8.2.2 on page 270. > > The following paragraph has me puzzled: > > "Note the subtle but crucial difference between instance_eval and > class_eval when the code being evaluated contains a method definition. > instance_eval defines singleton methods of the object (and this results > in class methods when it is called on a class object). class_eval > defines regular instance methods." > > Is this not supposed to be the other way around? instance_eval should > define instance methods and class_eval should define class methods. Is > there a catch here? The catch is that a class is an instance. For a moment, forget that class_eval even exists. Without that distraction, instance_eval seems to operate consistently: 1) it creates singleton methods for 'normal 'objects, e.g. mydog which is an instance of a Dog class, and 2) it creates singleton methods for objects which are instances of the class Class, i.e. all classes. It just so happens that a singleton method for a class object is known as 'class method'. -- Posted via http://www.ruby-forum.com/.