From: "David A. Black" Date: 2004-03-09T01:33:43+09:00 Subject: Re: proposal: class< On Tue, 09 Mar 2004 00:02:13 +0900, Carlos wrote: > [snip] > > Maybe you should ask for a method #singleton for singleton classes, > > that returns the object... ;) > > I tried poking around with 'singleton'.. but no luck. > Please give me some more hints. > > ;-) > > server> ruby a.rb > #> > a.rb:7: warning: instance variable @str not initialized > nil > server> cat a.rb > obj = "text" > class << obj > def init > @str = "hello" > end > def hello > puts @str > end > p self > #p self.methods > #p self.singleton_methods > end > obj.hello Here's something which might suggest some possibilities for the kind of thing you want to do. It shows a way to keep both an object and its singleton class in scope: obj = Object.new c = (class << obj; self; end) c.class_eval do define_method("x") do puts "I am an object: #{self}" puts "I'm assigned to the variable obj: #{obj}" puts "My singleton class is a class: #{c}" puts "My historical class is also a class: #{self.class}" end end obj.x => I am an object: # I'm assigned to the variable obj: # My singleton class is a class: #> My historical class is also a class: Object David -- David A. Black dblack@wobblini.net