From: ara.t.howard@... Date: 2007-02-15T05:11:03+09:00 Subject: Re: Creating a second instance of a singleton class? 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" > Obviously #4 is the place where I'm stuck. B/c I'm stuck, I am (as you > suggest) using module mix-in's to solve the problem, but it's less clean due > to my context in the code.. It'll work, but not as nicely as if there were a > method such as: > > Test2 = test1.dup_singleton_class > test2 = Test2.new > > I can't see why Ruby would object inherently to copying all the class methods > of a singleton into a new class object (which is all I want - just a template > of the singleton class's methods - no class variables or whatnot). you have to have the the class variables if you have class methods. otherwise this class C @x = 42 def self.x() @x end end won't work in strongly suspect there is an easier way to skin your cat. can you tell us, not what you are trying to do, but why? -a -- we can deny everything, except that we have the possibility of being better. simply reflect on that. - the dalai lama