From: ara.t.howard@... Date: 2007-02-15T05:25:11+09:00 Subject: Re: Creating a second instance of a singleton class? On Thu, 15 Feb 2007 Ara.T.Howard@noaa.gov wrote: > On Thu, 15 Feb 2007, Steve Midgley wrote: > >> dblack wrote: >> >>> I think the idea is for the object to have its own class, so if that >>> class ceased to be its own class, there would have to be some new >>> construct that was its own class, and you'd be back where you started >>> :-) So I don't think it's really possible in terms of the logic of >>> the model, and I can't think of any way to do it. >>> >>> Is it possible you could use modules to bring about the kind of thing >>> you're looking for? >> >> Thanks David - I think I understand what you're saying.. I'd happily settle >> for a *copy* of the instance's singleton class, but maybe I'm missing your >> point? >> >> Another way to describe what I want to do: >> >> 1) create a class "Test" >> 2) create an instance of Test: "test1" >> 3) modify test1's class definition, creating a singleton class >> 4) Somehow "promote" or copy this singleton class to be a full fledged >> class as "Test2" >> 5) Create an instance of "Test2" as "test2" > > harp:~ > cat a.rb > require 'rubygems' > require 'prototype' > > # 1) > class Test; end > > # 2) > test1 = Test.new > > # 3,4) > Test2 = Class.new(test1.class){ > def new_method > p 'no singleton class is needed' > end > } > > # 5) > test2 = Test2.new > > test2.new_method > > > harp:~ > ruby a.rb > "no singleton class is needed" and, if it is harp:~ > cat a.rb require 'rubygems' require 'prototype' # 1) class Test; end # 2) test1 = Test.new # 3) m = Module.new{ def new_method p 'if a singleton class is needed' end } (class << test1; self; end).module_eval{ include m } # much easier - no singleton class needed here either # test1.extend m # 4) Test2 = Class.new(test1.class){ include m } # 5) test2 = Test2.new test2.new_method harp:~ > ruby a.rb "if a singleton class is needed" -a -- we can deny everything, except that we have the possibility of being better. simply reflect on that. - the dalai lama