From: Chris Czub Date: 2007-10-26T04:42:01+09:00 Subject: Re: Need help understanding metaclassing Daniel Waite wrote: > Daniel Waite wrote: >> 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. > > This works: > > cs.instance_eval do > def eat > puts "You eat." > end > end > > cs.eat # You eat. > > What's up with define_method? The method is put into the Creature class and is inaccessible in the simpleton class. Try 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 c.eat -- Posted via http://www.ruby-forum.com/.