# Re: Creating a second instance of a singleton class?

**URL:** <https://www.ruby-forum.com/t/re-creating-a-second-instance-of-a-singleton-class/85593>\
**Category:** Ruby\
**Created:** [February 14, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/re-creating-a-second-instance-of-a-singleton-class/85593 "2007-02-14T20:56:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjmadsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@sjmadsen](https://www.ruby-forum.com/u/sjmadsen)\
**Post date:** [February 14, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/re-creating-a-second-instance-of-a-singleton-class/85593/1 "2007-02-14T20:56:29Z")

</div>

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”

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).

Thanks for the insight,

Steve

---

<div class="post-metadata">

**Author:** ![sjmadsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@sjmadsen](https://www.ruby-forum.com/u/sjmadsen)\
**Post date:** [February 14, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/re-creating-a-second-instance-of-a-singleton-class/85593/2 "2007-02-14T21:14:33Z")

</div>

On Thu, 15 Feb 2007, Steve M. wrote:

> 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”

> 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

---

<div class="post-metadata">

**Author:** ![sjmadsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@sjmadsen](https://www.ruby-forum.com/u/sjmadsen)\
**Post date:** [February 14, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/re-creating-a-second-instance-of-a-singleton-class/85593/3 "2007-02-14T21:25:34Z")

</div>

On Thu, 15 Feb 2007 removed\_email\_address@domain.invalid wrote:

> > > Is it possible you could use modules to bring about the kind of thing
> > 
> > 1. modify test1’s class definition, creating a singleton class
> 
> # 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

---

<div class="post-metadata">

**Author:** ![sjmadsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@sjmadsen](https://www.ruby-forum.com/u/sjmadsen)\
**Post date:** [February 15, 2007, 4:20am UTC](https://www.ruby-forum.com/t/re-creating-a-second-instance-of-a-singleton-class/85593/4 "2007-02-15T04:20:15Z")

</div>

Hi –

On Thu, 15 Feb 2007, removed\_email\_address@domain.invalid wrote:

> harp:~ \> cat a.rb  
> require ‘rubygems’  
> require ‘prototype’

Are you sure you need those for your example?

> def self.x() @x end  
> end
> 
> won’t work

That’s not a class variable, though – but I’m a bit confused by  
Steve’s reference to class methods. Steve, why are you referring to  
class methods? 🙂

David
