From: Bill Kelly Date: 2006-04-12T03:02:48+09:00 Subject: Re: define_singleton_method(a_symbol,&block) From: > > On Tue, 11 Apr 2006, Bill Kelly wrote: > >> I was wondering what was the idiomatic way to do this: >> >> class BigDipper >> def little_dipper(a_symbol) >> define_singleton_method(a_symbol) { >> "#{a_symbol} is double-helix" >> } >> end >> end >> >> s_ = BigDipper.new >> s_.little_dipper(:DNA) >> s_.DNA >> => "DNA is a double-helix" >> >> q_ = BigDipper.new >> q_.respond_to? :DNA >> => false > > > harp:~ > cat a.rb > class BigDipper > def self.little_dipper(a_symbol) > define_method(a_symbol) { "#{a_symbol} is double-helix" } > end > def little_dipper(*a, &b) self.class.little_dipper(*a, &b) end > end > > s_ = BigDipper.new > s_.little_dipper(:DNA) > p s_.DNA Thanks, Ara. But is there any way to write it such that the method is only added to the specific instance of the class? I would like the following to return false: > q_ = BigDipper.new > p q_.respond_to?(:DNA) # => false Thanks, Bill