# Singleton method on object via define\_method?

**URL:** <https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537>\
**Category:** ruby-talk\
**Created:** [11 September 2004 09:48 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537 "2004-09-11T09:48:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [11 September 2004 09:48 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/1 "2004-09-11T09:48:07Z")

</div>

OK, I have this variable called box.

I want to define a singleton method xy on it. No problem.

However, I need a closure. So I need to use define\_method  
rather than def.

This is how I did it:

&nbsp;&nbsp;&nbsp;class \<\< box; self; end.class\_eval { define\_method(:xy) {[x,y]} }

Surely there is an easier way??

Hal

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [11 September 2004 10:40 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/2 "2004-09-11T10:40:59Z")

</div>

I don't think so 🙂 I asked this a long time ago; Guy Decoux provided the  
same answer at RubyTalk:68053 and I wrote it up at

> **[WebMD - Better information. Better health.](https://www.webmd.com/?SingletonTutorial)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

Regards,

Brian.

> **···**
>
> On Sat, Sep 11, 2004 at 06:48:07PM +0900, Hal Fulton wrote:
> 
> > OK, I have this variable called box.
> > 
> > I want to define a singleton method xy on it. No problem.
> > 
> > However, I need a closure. So I need to use define\_method  
> > rather than def.
> > 
> > This is how I did it:
> > 
> > &nbsp;&nbsp;class \<\< box; self; end.class\_eval { define\_method(:xy) {[x,y]} }
> > 
> > Surely there is an easier way??

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [11 September 2004 11:02 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/3 "2004-09-11T11:02:09Z")

</div>

Off the top of my head I use a common lib:

module Kernal

&nbsp;&nbsp;def metaclass  
&nbsp;&nbsp;&nbsp;&nbsp;(class \<\< self; self; end)  
&nbsp;&nbsp;end

&nbsp;&nbsp;def metaclass\_eval(&block)  
&nbsp;&nbsp;&nbsp;&nbsp;(class \<\< self; self; end).class\_eval(&block)  
&nbsp;&nbsp;end

&nbsp;&nbsp;def define\_singleton(meth, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;metaclass\_eval { define\_method(meth, &block) }  
&nbsp;&nbsp;end

end

T.

> **···**
>
> On Saturday 11 September 2004 05:48 am, Hal Fulton wrote:
> 
> > OK, I have this variable called box.
> > 
> > I want to define a singleton method xy on it. No problem.
> > 
> > However, I need a closure. So I need to use define\_method  
> > rather than def.
> > 
> > This is how I did it:
> > 
> > &nbsp;&nbsp;&nbsp;class \<\< box; self; end.class\_eval { define\_method(:xy) {[x,y]} }
> > 
> > Surely there is an easier way??
> 
> --  
> ( o \_  
> // trans.  
> / \ transami@runbox.com
> 
> I don't give a damn for a man that can only spell a word one way.  
> -Mark Twain

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [11 September 2004 12:19 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/4 "2004-09-11T12:19:45Z")

</div>

Hi --

> **···**
>
> On Sat, 11 Sep 2004, Hal Fulton wrote:
> 
> > OK, I have this variable called box.
> > 
> > I want to define a singleton method xy on it. No problem.
> > 
> > However, I need a closure. So I need to use define\_method  
> > rather than def.
> > 
> > This is how I did it:
> > 
> > &nbsp;&nbsp;&nbsp;class \<\< box; self; end.class\_eval { define\_method(:xy) {[x,y]} }
> > 
> > Surely there is an easier way??
> 
> If my Kernel#singleton\_class RCR gets accepted (or if you write an ad  
> hoc version of it), you'll at least be able to shorten it to:
> 
> &nbsp;&nbsp;box.singleton\_class.class\_eval { ... }
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [12 September 2004 11:10 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/5 "2004-09-12T11:10:05Z")

</div>

"trans. (T. Onoma)" \<transami@runbox.com\> schrieb im Newsbeitrag news:200409110701.54011.transami@runbox.com...

> **···**
>
> > On Saturday 11 September 2004 05:48 am, Hal Fulton wrote:
> > 
> > > OK, I have this variable called box.
> > > 
> > > I want to define a singleton method xy on it. No problem.
> > > 
> > > However, I need a closure. So I need to use define\_method  
> > > rather than def.
> > > 
> > > This is how I did it:
> > > 
> > > &nbsp;&nbsp;&nbsp;class \<\< box; self; end.class\_eval { define\_method(:xy) {[x,y]} }
> > > 
> > > Surely there is an easier way??
> > 
> > Off the top of my head I use a common lib:
> > 
> > module Kernal
> > 
> > def metaclass  
> > &nbsp;&nbsp;&nbsp;(class \<\< self; self; end)  
> > end
> > 
> > def metaclass\_eval(&block)  
> > &nbsp;&nbsp;&nbsp;(class \<\< self; self; end).class\_eval(&block)  
> > end
> > 
> > def define\_singleton(meth, &block)  
> > &nbsp;&nbsp;&nbsp;metaclass\_eval { define\_method(meth, &block) }  
> > end
> > 
> > end
> 
> Good! I'd just put it into class Object instead of module Kernel. That's a more appropriate place IMHO. Also "metaclass" seems the wrong term in this case, although I can see why you didn't want to use "singleton\_class"...
> 
> Regards
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [12 September 2004 12:20 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/6 "2004-09-12T12:20:06Z")

</div>

Agreed. Better name?

> **···**
>
> On Sunday 12 September 2004 07:10 am, Robert Klemme wrote:
> 
> > "trans. (T. Onoma)" \<transami@runbox.com\> schrieb im Newsbeitrag  
> > \> module Kernal  
> > \>  
> > \> def metaclass  
> > \> (class \<\< self; self; end)  
> > \> end  
> > \>  
> > \> def metaclass\_eval(&block)  
> > \> (class \<\< self; self; end).class\_eval(&block)  
> > \> end  
> > \>  
> > \> def define\_singleton(meth, &block)  
> > \> metaclass\_eval { define\_method(meth, &block) }  
> > \> end  
> > \>  
> > \> end
> > 
> > Good! I'd just put it into class Object instead of module Kernel. That's  
> > a more appropriate place IMHO. Also "metaclass" seems the wrong term in  
> > this case, although I can see why you didn't want to use  
> > "singleton\_class"...
> 
> --  
> ( o \_  
> // trans.  
> / \ transami@runbox.com
> 
> I don't give a damn for a man that can only spell a word one way.  
> -Mark Twain

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [12 September 2004 18:19 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/7 "2004-09-12T18:19:56Z")

</div>

selfclass?

martin

> **···**
>
> "trans. (T. Onoma)" \<transami@runbox.com\> wrote:  
> \>
> 
> > \> Good! I'd just put it into class Object instead of module Kernel. That's  
> > \> a more appropriate place IMHO. Also "metaclass" seems the wrong term in  
> > \> this case, although I can see why you didn't want to use  
> > \> "singleton\_class"...
> > 
> > Agreed. Better name?

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [12 September 2004 19:14 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/8 "2004-09-12T19:14:17Z")

</div>

That's better. Or maybe self\_class.

That could work.

> **···**
>
> On Sunday 12 September 2004 02:19 pm, Martin DeMello wrote:
> 
> > "trans. (T. Onoma)" \<transami@runbox.com\> wrote:  
> > \> \> Good! I'd just put it into class Object instead of module Kernel.  
> > \> \> That's a more appropriate place IMHO. Also "metaclass" seems the wrong  
> > \> \> term in this case, although I can see why you didn't want to use  
> > \> \> "singleton\_class"...  
> > \>  
> > \> Agreed. Better name?
> > 
> > selfclass?
> 
> --  
> ( o \_  
> // trans.  
> / \ transami@runbox.com
> 
> I don't give a damn for a man that can only spell a word one way.  
> -Mark Twain

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [13 September 2004 02:07 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/9 "2004-09-13T02:07:12Z")

</div>

Hi --

> \> \> \> Good! I'd just put it into class Object instead of module Kernel.  
> \> \> \> That's a more appropriate place IMHO. Also "metaclass" seems the wrong  
> \> \> \> term in this case, although I can see why you didn't want to use  
> \> \> \> "singleton\_class"...  
> \> \>  
> \> \> Agreed. Better name?  
> \>  
> \> selfclass?
> 
> That's better. Or maybe self\_class.

Then you have "self.class" and "self.self\_class" -- or, for that  
matter, potentially "self.class" and "self\_class" -- meaning different  
things but looking awfully similar.

I think Matz is actually migrating from 'singleton class' to 'virtual  
class'. I don't like that term very much (there's nothing really  
"virtual" about it; it's a real class, once it's created, and if it's  
not created, it's not even virtual), but it's probably a good idea to  
keep the terminology unified.

David

> **···**
>
> On Mon, 13 Sep 2004, trans. (T. Onoma) wrote:
> 
> > On Sunday 12 September 2004 02:19 pm, Martin DeMello wrote:  
> > \> "trans. (T. Onoma)" \<transami@runbox.com\> wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [13 September 2004 02:24 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/10 "2004-09-13T02:24:59Z")

</div>

D. A. Black wrote:

> I think Matz is actually migrating from 'singleton class' to 'virtual  
> class'. I don't like that term very much (there's nothing really  
> "virtual" about it; it's a real class, once it's created, and if it's  
> not created, it's not even virtual), but it's probably a good idea to  
> keep the terminology unified.

Interesting. "It's a real class, once it's created, ..." On what grounds  
do you deem it to be a "real" class?

And if there were such a thing as a "virtual class", what would it be?

Cheers,  
Gavin

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [13 September 2004 02:46 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/11 "2004-09-13T02:46:32Z")

</div>

> D. A. Black wrote:  
> \> I think Matz is actually migrating from 'singleton class' to 'virtual  
> \> class'. I don't like that term very much (there's nothing really  
> \> "virtual" about it; it's a real class, once it's created, and if it's  
> \> not created, it's not even virtual), but it's probably a good idea to  
> \> keep the terminology unified.
> 
> Interesting. "It's a real class, once it's created, ..." On what grounds  
> do you deem it to be a "real" class?

irb(main):001:0\> c = (class \<\< ""; self; end)  
=\> #\<Class:#\<String:0x402a8294\>\>  
irb(main):002:0\> c.class  
=\> Class

> And if there were such a thing as a "virtual class", what would it be?

I have no idea. The term doesn't evoke anything to me.

David

> **···**
>
> On Mon, 13 Sep 2004, Gavin Sinclair wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [13 September 2004 05:40 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/12 "2004-09-13T05:40:59Z")

</div>

I don't know what a "virtual class" would be in real terms, but  
semantically, I'd say it's some kind of object benignly masqerading as a  
class. Like a proxy object for a remote Froboz object might be said to  
masquerade as a Froboz.

So just because your object above tells the world it's a "Class", I'm not  
necessarily convinced. I realise that, if this \_is\_ a benign deception,  
it's a deep one, because you'd expect the #class method to be truthful.  
However, that's to be expected when we're talking about the deep internals  
of Ruby.

If the singleton class of an object is not treated exactly the same as a  
"normal" class in Ruby's implementation, then I think there's some  
justification for the word "virtual".

Gavin

> **···**
>
> > On Mon, 13 Sep 2004, Gavin Sinclair wrote:
> > 
> > > D. A. Black wrote:  
> > > \> I think Matz is actually migrating from 'singleton class' to  
> > > 'virtual class'. I don't like that term very much (there's nothing  
> > > really "virtual" about it; it's a real class, once it's created, and  
> > > if it's not created, it's not even virtual), but it's probably a  
> > > good idea to keep the terminology unified.
> > > 
> > > Interesting. "It's a real class, once it's created, ..." On what  
> > > grounds do you deem it to be a "real" class?
> > 
> > irb(main):001:0\> c = (class \<\< ""; self; end)  
> > =\> #\<Class:#\<String:0x402a8294\>\>  
> > irb(main):002:0\> c.class  
> > =\> Class
> 
> > > And if there were such a thing as a "virtual class", what would it be?
> > 
> > I have no idea. The term doesn't evoke anything to me.

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [13 September 2004 05:45 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/13 "2004-09-13T05:45:26Z")

</div>

Gavin Sinclair wrote:

> I don't know what a "virtual class" would be in real terms, but  
> semantically, I'd say it's some kind of object benignly masqerading as a  
> class. Like a proxy object for a remote Froboz object might be said to  
> masquerade as a Froboz.
> 
> So just because your object above tells the world it's a "Class", I'm not  
> necessarily convinced. I realise that, if this \_is\_ a benign deception,  
> it's a deep one, because you'd expect the #class method to be truthful. However, that's to be expected when we're talking about the deep internals  
> of Ruby.
> 
> If the singleton class of an object is not treated exactly the same as a  
> "normal" class in Ruby's implementation, then I think there's some  
> justification for the word "virtual".

I really think it's just a class. It has special uses, perhaps, but I don't  
think it's treated specially. I can't see that it's anything other than a  
real Class.

Hal

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [13 September 2004 12:51 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/14 "2004-09-13T12:51:56Z")

</div>

Hi --

> \>  
> \>\> D. A. Black wrote:  
> \>\> \> I think Matz is actually migrating from 'singleton class' to  
> \>\> 'virtual class'. I don't like that term very much (there's nothing  
> \>\> really "virtual" about it; it's a real class, once it's created, and  
> \>\> if it's not created, it's not even virtual), but it's probably a  
> \>\> good idea to keep the terminology unified.  
> \>\>  
> \>\> Interesting. "It's a real class, once it's created, ..." On what  
> \>\> grounds do you deem it to be a "real" class?  
> \>  
> \> irb(main):001:0\> c = (class \<\< ""; self; end)  
> \> =\> #\<Class:#\<String:0x402a8294\>\>  
> \> irb(main):002:0\> c.class  
> \> =\> Class
> 
> \>\> And if there were such a thing as a "virtual class", what would it be?  
> \>  
> \> I have no idea. The term doesn't evoke anything to me.
> 
> I don't know what a "virtual class" would be in real terms, but  
> semantically, I'd say it's some kind of object benignly masqerading as a  
> class. Like a proxy object for a remote Froboz object might be said to  
> masquerade as a Froboz.
> 
> So just because your object above tells the world it's a "Class", I'm not  
> necessarily convinced. I realise that, if this \_is\_ a benign deception,  
> it's a deep one, because you'd expect the #class method to be truthful.  
> However, that's to be expected when we're talking about the deep internals  
> of Ruby.
> 
> If the singleton class of an object is not treated exactly the same as a  
> "normal" class in Ruby's implementation, then I think there's some  
> justification for the word "virtual".

I think this introduces an unnecessary level of complexity. You're  
taking the long way around -- it's a Class object, but it's not a  
class, but it acts like one, so we might as well say it is -- when in  
fact it's simply a Class object. There's nothing more or less  
"normal" about it than about other Class objects; the whole per-object  
behavior model of Ruby is based on the idea that every object has both  
a class of origin and a class of its own where the definitions  
exclusive to that object reside. This isn't abnormal, nor an  
aberration; it's the way the whole thing works.

David

> **···**
>
> On Mon, 13 Sep 2004, Gavin Sinclair wrote:
> 
> > \> On Mon, 13 Sep 2004, Gavin Sinclair wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [13 September 2004 07:13 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/15 "2004-09-13T07:13:08Z")

</div>

Except that it is tied to the object and not the class hierarchy. So it is  
'virtually' a class, but not quite 😉

> **···**
>
> On Monday 13 September 2004 01:45 am, Hal Fulton wrote:
> 
> > Gavin Sinclair wrote:  
> > \> I don't know what a "virtual class" would be in real terms, but  
> > \> semantically, I'd say it's some kind of object benignly masqerading as a  
> > \> class. Like a proxy object for a remote Froboz object might be said to  
> > \> masquerade as a Froboz.  
> > \>  
> > \> So just because your object above tells the world it's a "Class", I'm not  
> > \> necessarily convinced. I realise that, if this \_is\_ a benign deception,  
> > \> it's a deep one, because you'd expect the #class method to be truthful.  
> > \> However, that's to be expected when we're talking about the deep  
> > \> internals of Ruby.  
> > \>  
> > \> If the singleton class of an object is not treated exactly the same as a  
> > \> "normal" class in Ruby's implementation, then I think there's some  
> > \> justification for the word "virtual".
> > 
> > I really think it's just a class. It has special uses, perhaps, but I don't  
> > think it's treated specially. I can't see that it's anything other than a  
> > real Class.
> 
> --  
> ( o \_  
> // trans.  
> / \ transami@runbox.com
> 
> I don't give a damn for a man that can only spell a word one way.  
> -Mark Twain

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [13 September 2004 13:24 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/16 "2004-09-13T13:24:58Z")

</div>

"David A. Black" \<dblack@wobblini.net\> schrieb im Newsbeitrag  
news:Pine.LNX.4.44.0409130546200.13044-100000@wobblini...

> Hi --
> 
> \> So just because your object above tells the world it's a "Class", I'm

not

> \> necessarily convinced. I realise that, if this \_is\_ a benign

deception,

> \> it's a deep one, because you'd expect the #class method to be

truthful.

> \> However, that's to be expected when we're talking about the deep

internals

> \> of Ruby.  
> \>  
> \> If the singleton class of an object is not treated exactly the same as

a

> \> "normal" class in Ruby's implementation, then I think there's some  
> \> justification for the word "virtual".

Originally I was going to argue that "virtual" is inappropriate because it  
was deceiving. While thinking about this issue it occurred to me that  
"singleton classes" aka "virtual classes" share a property with C++'s  
"virtual classes": you cannot instantiate objects from them.

OTOH, you can have exactly one instance of this class, but this instance  
preceedes the class (i.e. first the instance is created and then - maybe -  
the class). Strictly speaking, the instance is the factory for the  
class - not the other way round as it is normally.

\<philosophical\>I depends on everyones notion of "class" whether he or she  
thinks this no longer makes it a class. Although in that case I'd be  
interested to learn a more appropriate name for this  
thing.\</philosophical\>

> I think this introduces an unnecessary level of complexity. You're  
> taking the long way around -- it's a Class object, but it's not a  
> class, but it acts like one, so we might as well say it is -- when in  
> fact it's simply a Class object. There's nothing more or less  
> "normal" about it than about other Class objects;

I regard the lack of the ability to create instances a major difference.  
Just as a reminder

> > s="foo"

=\> "foo"

> > class \<\< s; new ; end

TypeError: can't create instance of virtual class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6:in `new'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6

> > class \<\< s; allocate ; end

TypeError: can't create instance of virtual class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):7:in `allocate'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):7

> > class \<\< s; ancestors ; end

=\> [String, Enumerable, Comparable, Object, Kernel]

> the whole per-object  
> behavior model of Ruby is based on the idea that every object has both  
> a class of origin and a class of its own where the definitions  
> exclusive to that object reside. This isn't abnormal, nor an  
> aberration; it's the way the whole thing works.

Well, it's normal in Ruby. But generally speaking the naive OO expert  
would expect either not to have singleton/virtual classes (SVC :-)) \*or\*  
have Object#class return the singleton class - if that would exist. Note,  
I'm not advocating to change this in any way - in fact it's good the way  
it is. I just try to uncover why SVC often create confusion.

The schizophrenic thing about SVC is, that instances change their class at  
most once without showing it to the outside world (i.e. #class still  
returns the original class).

All this shows, what weired things men can come up with - that in practice  
do work really great. 🙂

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

PS: Sorry for thinking aloud...

> **···**
>
> > On Mon, 13 Sep 2004, Gavin Sinclair wrote:

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [13 September 2004 07:22 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/17 "2004-09-13T07:22:00Z")

</div>

trans. (T. Onoma) wrote:

> > I really think it's just a class. It has special uses, perhaps, but I don't  
> > think it's treated specially. I can't see that it's anything other than a  
> > real Class.
> 
> Except that it is tied to the object and not the class hierarchy. So it is 'virtually' a class, but not quite 😉

You can't create an instance of it, and you can't subclass it.  
Those are the only differences I'm aware of with other classes.

The class Class can't be subclassed either. Is Class not a real class?  
It is. It's just a special case.

Hal

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [13 September 2004 14:04 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/18 "2004-09-13T14:04:55Z")

</div>

self#protoclass ?

martin

> **···**
>
> Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> > OTOH, you can have exactly one instance of this class, but this instance  
> > preceedes the class (i.e. first the instance is created and then - maybe -  
> > the class). Strictly speaking, the instance is the factory for the  
> > class - not the other way round as it is normally.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [13 September 2004 16:12 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/19 "2004-09-13T16:12:56Z")

</div>

Hi --

> Originally I was going to argue that "virtual" is inappropriate because it  
> was deceiving. While thinking about this issue it occurred to me that  
> "singleton classes" aka "virtual classes" share a property with C++'s  
> "virtual classes": you cannot instantiate objects from them.

Actually if C++ has virtual classes, I think that's all the more  
reason for Ruby not to call them that. Any such point of contact  
inevitably leads to misunderstanding and/or expectation (i.e., that  
Ruby will be like the other language).

> OTOH, you can have exactly one instance of this class, but this instance  
> preceedes the class (i.e. first the instance is created and then - maybe -  
> the class). Strictly speaking, the instance is the factory for the  
> class - not the other way round as it is normally.
> 
> \<philosophical\>I depends on everyones notion of "class" whether he or she  
> thinks this no longer makes it a class. Although in that case I'd be  
> interested to learn a more appropriate name for this  
> thing.\</philosophical\>
> 
> \> [I wrote:]  
> \>  
> \> the whole per-object  
> \> behavior model of Ruby is based on the idea that every object has both  
> \> a class of origin and a class of its own where the definitions  
> \> exclusive to that object reside. This isn't abnormal, nor an  
> \> aberration; it's the way the whole thing works.
> 
> Well, it's normal in Ruby. But generally speaking the naive OO expert  
> would expect either not to have singleton/virtual classes (SVC :-)) \*or\*  
> have Object#class return the singleton class - if that would exist. Note,  
> I'm not advocating to change this in any way - in fact it's good the way  
> it is. I just try to uncover why SVC often create confusion.

"Naive OO expert" -- interesting 🙂 "Normal in Ruby" is good enough  
for me, since we're talking about Ruby. Otherwise it becomes an  
argument that there's an immutable/official OO model that languages  
must not deviate from. (I know you don't really think this -- I'm  
just extrapolating from what you're saying.)

> The schizophrenic thing about SVC is, that instances change their class at  
> most once without showing it to the outside world (i.e. #class still  
> returns the original class).

I think we're looking at this backwards in this discussion. We're  
looking at it as: Objects have this weird class-like thing, so what  
is it? I think it's better to look at it from the other end, like  
this: Objects in Ruby can have methods added to them on a per-object  
basis. That's a fundamental principle of Ruby (whether it is true of  
other OO languages or not). The way Matz has chosen to implement this  
is by associating a second, dedicated class with each object. That's  
what this class actually \*is\* -- it's the fulfillment of that design,  
not something growing off the design that has to be explained  
separately.

The details then fall into place. The fact that the class doesn't  
exist until it's needn't does not have to mean that there's anything  
virtual about it -- it's just a matter of efficiency, and it's  
transparent. And it's a perfectly real class, because it fits the  
definition of what a class can be in Ruby.

Hmmm... "dedicated\_class"... hmmm... 🙂

David

> **···**
>
> On Mon, 13 Sep 2004, Robert Klemme wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Randy\_W\_Sims](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Randy\_W\_Sims](https://rubytalk.org/u/Randy_W_Sims)\
**Post date:** [13 September 2004 08:09 UTC](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537/20 "2004-09-13T08:09:16Z")

</div>

Hal Fulton wrote:

> trans. (T. Onoma) wrote:
> 
> > > I really think it's just a class. It has special uses, perhaps, but I don't  
> > > think it's treated specially. I can't see that it's anything other than a  
> > > real Class.
> > 
> > Except that it is tied to the object and not the class hierarchy. So it is 'virtually' a class, but not quite 😉
> 
> You can't create an instance of it, and you can't subclass it.  
> Those are the only differences I'm aware of with other classes.
> 
> The class Class can't be subclassed either. Is Class not a real class?  
> It is. It's just a special case.

Sounds sort of like a traits[1] or what Perl 6 refers to as roles[2][3]. But, then this is deeper Ruby than I'm familiar with.

Randy.

1. \<[http://www.cse.ogi.edu/~black/publications/TR\_CSE\_02-012.pdf&gt](http://www.cse.ogi.edu/~black/publications/TR_CSE_02-012.pdf&gt);  
2. \<[GitHub - Raku/old-design-docs: Raku language design documents](http://dev.perl.org/perl6/apocalypse/A12.html&gt);  
3. \<[http://search.cpan.org/~lpalmer/Class-Role-0.03/Role.pm&gt](http://search.cpan.org/~lpalmer/Class-Role-0.03/Role.pm&gt);

[Next page](https://rubytalk.org/t/singleton-method-on-object-via-define-method/13537.md?page=2)
