From: Logan Capaldo Date: 2006-05-25T03:37:39+09:00 Subject: Re: define_method for a single instance? On May 24, 2006, at 2:18 PM, Jake McArthur wrote: > I'm trying to define a method for a specific instance of a class > rather than for every instance of the class. Basically, something > like this: > > class A > def add_wibble_method > # don't know what to do here > end > end > > foo = A.new > bar = A.new > foo.add_wibble_method > foo.wibble # should work > # bar.wibble should not work > > Any help would be appreciated. > > - Jake McArthur > % cat wibble.rb class A def add_wibble def self.wibble puts "I'm wibbling!" end end end class B def add_wibble (class << self; self; end).class_eval do define_method(:wibble) { puts "I too can wibble." } end end end a = A.new b = B.new a.add_wibble a.wibble b.add_wibble b.wibble begin a1 = A.new a1.wibble rescue => ex puts ex end begin b1 = B.new b1.wibble rescue => ex puts ex end % ruby wibble.rb I'm wibbling! I too can wibble. undefined method `wibble' for # undefined method `wibble' for #