Would it make sense for "def method" instance evaled on a class to create an instance method?

def in a class definition (where self is the class) creates an instance
method.

class Class1
  def method1
  end
end

Class1.new.method1

but if instance evaled on Class1, creates a singleton method.

Class1.instance_eval do
  def method2
  end
end

should there perhaps be an exception for classes (for non classes a
singleton method is the only option), where an unqualified def would
create an instance method, same as in class definition?

···

--
Posted via http://www.ruby-forum.com/.

Hi,

this is the very difference between instance_eval and class_eval: A
"def" expression in instance_eval will always create a singleton method.
If you want an instance method (i. e. the same behaviour as inside a
class body), you have to use class_eval.

This is explained further in the book "The Ruby programming language":

http://books.google.de/books?id=jcUbTcr5XWwC&pg=PA270

···

--
Posted via http://www.ruby-forum.com/.