Hi --

On Tue, 9 Mar 2004, Simon Strandgaard wrote:

> 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
> #<Class:#<String:0x810a9b8>>
> 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: #<Object:0x401b8d40>
    I'm assigned to the variable obj: #<Object:0x401b8d40>
    My singleton class is a class: #<Class:#<Object:0x401b8d40>>
    My historical class is also a class: Object


David

-- 
David A. Black
dblack / wobblini.net