From: ara.t.howard@... Date: 2006-04-11T22:46:40+09:00 Subject: Re: define_singleton_method(a_symbol,&block) On Tue, 11 Apr 2006, Bill Kelly wrote: > Hi, > > 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 q_ = BigDipper.new p q_.respond_to?(:DNA) BigDipper.little_dipper 'DNA2' p s_.respond_to?('DNA2') p q_.respond_to?('DNA2') harp:~ > ruby a.rb "DNA is double-helix" true true true hth. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama