From: Daniel Waite Date: 2007-10-26T04:28:43+09:00 Subject: Re: Need help understanding metaclassing Rick Denatale wrote: > class Foo > metaclass #=> the singleton class of Foo. > metaclass.instance_eval do > # Inside this block self is bound to the singleton class of > foo > define_method(:bar) {"bar"} > end > end > > This has the same effect as > class Foo > def self.bar > "bar" > end > end > > I.e. it defines a class method of Foo. > > Now I'd usually use class_eval, or module_eval which do the same thing > but only work if the receiver is a class/module. Hold on, now I'm confused. Given this... class Creature def singleton class << self self end end end c = Creature.new cs = c.singleton cs.instance_eval do define_method(:eat) do puts "You eat." end end cs.eat # => errors: NoMethodError: undefined method `eat' for #> Why is that? I've tried class_eval, too and I get the same thing. I thought I understood this stuff pretty well but I guess not. -- Posted via http://www.ruby-forum.com/.