# Creating a second instance of a singleton class?

**URL:** <https://www.ruby-forum.com/t/creating-a-second-instance-of-a-singleton-class/85588>\
**Category:** Ruby\
**Created:** [February 14, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/creating-a-second-instance-of-a-singleton-class/85588 "2007-02-14T20:24:26Z")\
**Posts on this page:** 9\
**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:24pm UTC](https://www.ruby-forum.com/t/creating-a-second-instance-of-a-singleton-class/85588/1 "2007-02-14T20:24:26Z")

</div>

Hey,

I’ve been working on a weird little problem that I thought people on  
this list might have a quick answer for (though the answer may be  
“impossible!”):

[code starts]

class Test; end

test = Test.new

class \<\< test  
def new\_method  
puts “new method is accessible!”  
end  
end

test.new\_method # works  
test1 = test.dup # works but creates class from original class not  
singleton  
test2 = class \<\< test; self.new; end; # blows up with “can’t create  
instance of virtual class (TypeError)”

# neither of these will therefore work

test1.new\_method  
test2.new\_method

[code ends]

Basically, I have a class “Test” and I create an instance of it. I  
modify the instance’s class definition (creating a singleton class  
definition).

Now what I really want is to create a second instance of the newly  
created singleton class. It appears to be impossible. A guess the name  
singleton implies as much. But with all of Ruby’s tricks, I thought  
there’d be a way to create a new instance of a singleton… Probably  
there is but I don’t see it.

Hmm…

Steve

p.s. I’ve simplified the use-case way down - there are external reasons  
why I want to modify the singleton and create an instance of it, rather  
than modifying the parent class (or at least I think there are)…

---

<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:34pm UTC](https://www.ruby-forum.com/t/creating-a-second-instance-of-a-singleton-class/85588/2 "2007-02-14T20:34:26Z")

</div>

Hi –

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

> # neither of these will therefore work
> 
> implies as much. But with all of Ruby’s tricks, I thought there’d be a way to  
> create a new instance of a singleton… Probably there is but I don’t see it.
> 
> Hmm…
> 
> Steve
> 
> p.s. I’ve simplified the use-case way down - there are external reasons why I  
> want to modify the singleton and create an instance of it, rather than  
> modifying the parent class (or at least I think there are)…

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?

David

---

<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, 10:43pm UTC](https://www.ruby-forum.com/t/creating-a-second-instance-of-a-singleton-class/85588/3 "2007-02-14T22:43:44Z")

</div>

Steve M. schrieb:

> I’ve been working on a weird little problem that I thought people on  
> this list might have a quick answer for (though the answer may be  
> “impossible!”):

Steve, don’t give up so fast, this is Ruby!

class Test; end

test = Test.new

class \<\< test  
def new\_method  
puts “new method is accessible!”  
end  
end

test.new\_method # =\> new method is accessible!

test1 = test.clone  
test1.new\_method # =\> new method is accessible!

Regards,  
Pit

---

<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, 11:54am UTC](https://www.ruby-forum.com/t/creating-a-second-instance-of-a-singleton-class/85588/4 "2007-02-15T11:54:14Z")

</div>

> Posted by Steve M. (stevemidgley) on 14.02.2007 20:24  
> …  
> Now what I really want is to create a second instance of the newly  
> created singleton class. It appears to be impossible. A guess the name  
> singleton implies as much. But with all of Ruby’s tricks, I thought  
> there’d be a way to create a new instance of a singleton… Probably  
> there is but I don’t see it.  
> …

Maybe also check out [What's wrong with this snippet - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/94696)

Regards  
JK

---

<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, 3:53pm UTC](https://www.ruby-forum.com/t/creating-a-second-instance-of-a-singleton-class/85588/5 "2007-02-15T15:53:18Z")

</div>

On 2/15/07, sur max [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hey guys !!  
> pls explain the meaning of Singleton Class  
> my perception is that it can only be associated with a single object, and  
> thats why its name so.  
> If you really wanna create more objects then isnt it better to redefine the  
> class with new functions.

Sorry to bore you , if you are already aware of this:

In Ruby when you add methods to an obj like this:

class \<\< obj  
def foobar; end  
end

it creates a Singleton/Virtual class for that object, if singleton  
class exists for that object, it would simply add method “foobar” to  
that. So basically, Steve wants to copy this singleton class  
associated with obj to the new class. Although its not clear to me,  
how that would enable him to dynamically change the underlying  
ActiveRecord table.

However, what puzzles me, how come #clone copies methods from  
Singleton class as well? #dup won’t allow that. You can’t marshal  
singleton and an attempt would throw error.

As usual Ara’s solution seems best, but if faced with similar  
situation I would probably also consider Delegator pattern.

---

<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, 12:05pm UTC](https://www.ruby-forum.com/t/creating-a-second-instance-of-a-singleton-class/85588/6 "2007-02-15T12:05:51Z")

</div>

hey guys !!  
pls explain the meaning of Singleton Class  
my perception is that it can only be associated with a single object,  
and  
thats why its name so.  
If you really wanna create more objects then isnt it better to redefine  
the  
class with new functions.

what say ?

---

<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:53pm UTC](https://www.ruby-forum.com/t/creating-a-second-instance-of-a-singleton-class/85588/7 "2007-02-15T16:53:31Z")

</div>

hemant schrieb:

> However, what puzzles me, how come #clone copies methods from  
> Singleton class as well? #dup won’t allow that. You can’t marshal  
> singleton and an attempt would throw error.

Works as documented:

ri Object#clone  
ri Object#dup

It says: While +clone+ is used to duplicate an object, including its  
internal state, +dup+ typically uses the class of the descendent object  
to create the new instance.

Regards,  
Pit

---

<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, 5:28pm UTC](https://www.ruby-forum.com/t/creating-a-second-instance-of-a-singleton-class/85588/8 "2007-02-15T17:28:36Z")

</div>

On 2/15/07, Pit C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It says: While +clone+ is used to duplicate an object, including its  
> internal state, +dup+ typically uses the class of the descendent object  
> to create the new instance.

Hey Pit, Internal State was not quite so clear to me so i read up in  
ri and passed right over it, thanks for clearing it up.  
Also, then may be we should be able to, marshal the damn singletons  
also. But PickAxe says Singletons are literally invisible.

---

<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, 8:48pm UTC](https://www.ruby-forum.com/t/creating-a-second-instance-of-a-singleton-class/85588/9 "2007-02-15T20:48:04Z")

</div>

hemant schrieb:

> On 2/15/07, Pit C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > It says: While +clone+ is used to duplicate an object, including its  
> > internal state, +dup+ typically uses the class of the descendent object  
> > to create the new instance.
> 
> Hey Pit, Internal State was not quite so clear to me so i read up in  
> ri and passed right over it, thanks for clearing it up.  
> Also, then may be we should be able to, marshal the damn singletons  
> also. But PickAxe says Singletons are literally invisible.

Hemant, to be fair, I hadn’t thought of singleton classes, too, before  
actually trying it. In retrospect, it makes kind of sense, that the  
internal state of an object includes the singleton class’ methods. And  
if its the business of the object’s class to do the #dup, then it is  
clear that this can’t include singleton methods, because the class  
doesn’t know about them.

Being able to marshal classes and singleton classes would be very nice,  
but its not officially supported yet. There are workarounds to do this,  
though.

Regards,  
Pit
