Hi -- On Wed, 12 Apr 2006, Bill Kelly wrote: > From: <ara.t.howard / noaa.gov> >> >> 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 Try this: class BigDipper def little_dipper(a_symbol) (class << self; self; end).class_eval do define_method(a_symbol) { "#{a_symbol} is double-helix" } end end end David -- David A. Black (dblack / wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" coming in PDF April 15, and in paper May 5! http://www.manning.com/black