Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Singleton class, metaclass, eigenclass: what do they mean?

Reply
Thread Tools

Singleton class, metaclass, eigenclass: what do they mean?

 
 
Tony Arcieri
Guest
Posts: n/a
 
      12-02-2010
[Note: parts of this message were removed to make it a legal post.]

Every time I think I have my head around what these terms mean I seem to run
across someone with a completely different definition.

My understanding was that the singleton class is what you obtain when you
call self.class in instance scope, and that metaclass and eigenclass
are interchangeable terms for what you obtain if you call class << self;
self; end in instance scope.

Is this correct? Do you have a different definition?

--
Tony Arcieri
Medioh! A Kudelski Brand

 
Reply With Quote
 
 
 
 
David Masover
Guest
Posts: n/a
 
      12-02-2010
On Wednesday, December 01, 2010 11:25:35 pm Tony Arcieri wrote:
> Every time I think I have my head around what these terms mean I seem to
> run across someone with a completely different definition.
>
> My understanding was that the singleton class is what you obtain when you
> call self.class in instance scope,


That doesn't make sense. I'm curious to know where you got that definition,
because when I call self.class, I just get the class, nothing "singleton"
about it. Maybe that's useful to distinguish it from the metaclass or
eigenclass, but I've always just called that the "class" of an object anyway.

Ruby's standard library has an implementation of the Singleton pattern, in
which there's a class which will only ever have one instance -- though this is
Ruby, so you can always cheat -- so I suppose if you had an instance of a
singleton class, calling self.class on that would give you a singleton class.

But calling self.class on something else, even something that's been
completely hacked up with extensions and even with directly modifying its
metaclass as below, is still going to give you the same class as you'd get
otherwise. That is, when I do this:

a = 'foo'
b = 'bar'

class << b
def to_sym
:hacked_bar
end
end

module C
def has_c?
true
end
end
b.extend C

Pretty much anything I do to b other than manually overriding the class
method, and I still can't tell the difference between a.class and b.class.
Both return String, and in every way I've cared to test, it's, well, just
String:

a.class # String
b.class # String
a.object_id == b.object_id # true
a.object_id == String.object_id # true

This is Ruby, so maybe I'm missing something, but it really does just seem to
be the class. Is that right?

> and that metaclass and eigenclass
> are interchangeable terms for what you obtain if you call class << self;
> self; end in instance scope.


This is correct, as far as I know. Note that you don't need to do this with
'self' necessarily -- you could do:

class << some_object; self; end

That would give you the metaclass of some_object.

I tend to use _why's metaid anyway.

 
Reply With Quote
 
 
 
 
Y. NOBUOKA
Guest
Posts: n/a
 
      12-02-2010
> My understanding was that the singleton class is what you obtain when you
> call self.class in instance scope


I think it is not a singleton class, but a normal class.

> metaclass and eigenclass are interchangeable terms for
> what you obtain if you call class << self; self; end in instance scope.


This is just an eigenclass, and I understand that the term
"eigenclass" is interchangeable with "singleton class".
Please see the following example:

class Object
# this method return the singleton class of the receiver
def singleton_class
class << self; self end
end
end

# obtain the singleton class of a String object
sc = "aaaa".singleton_class
p sc #=> #<Class:#<String:0x00000001442a50>>
begin
# if you try to create a instance of a singleton class...
sc.new # raise error!!
rescue => err
puts err #=> can't create instance of singleton class
# (ruby 1.9.1)
#=> can't create instance of virtual class
# (ruby 1.
# this error message tell us that +sc+ is
# an singleton class
end

I don't know the term "metaclass" is interchangeable with the term
"eigenclass"... I think a metaclass is a Class object or a singleton
class of a Class object, but I'm not sure.

--
nobuoka

 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      12-02-2010
[Note: parts of this message were removed to make it a legal post.]

On Wed, Dec 1, 2010 at 11:25 PM, Tony Arcieri <>wrote:

> Every time I think I have my head around what these terms mean I seem to
> run
> across someone with a completely different definition.
>
> My understanding was that the singleton class is what you obtain when you
> call self.class in instance scope, and that metaclass and eigenclass
> are interchangeable terms for what you obtain if you call class << self;
> self; end in instance scope.
>
> Is this correct? Do you have a different definition?
>
>

I take all three to mean the same thing: the object's own personal class. It
has only one instance, which is the object.

Or, in code:

class << obj
# now we are in obj's metaclass / eigenclass / singleton class
end

I think the different names exist for historical reasons, I personally use
"metaclass" because that is what _why used in Dwemthy's Array
http://mislav.uniqpath.com/poignant-guide/dwemthy/ but I think the name
"eigenclass" sounds badass (reminiscent of steampunk), so maybe I'll start
using it, instead

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      12-02-2010
On Thu, Dec 2, 2010 at 8:27 AM, Josh Cheek <> wrote:
> On Wed, Dec 1, 2010 at 11:25 PM, Tony Arcieri <>wr=

ote:
>
>> Every time I think I have my head around what these terms mean I seem to
>> run
>> across someone with a completely different definition.
>>
>> My understanding was that the singleton class is what you obtain when yo=

u
>> call self.class in instance scope, and that metaclass and eigenclass
>> are interchangeable terms for what you obtain if you call class << self;
>> self; end in instance scope.
>>
>> Is this correct? Do you have a different definition?
>>
>>

> I take all three to mean the same thing: the object's own personal class.=

It
> has only one instance, which is the object.
>
> Or, in code:
>
> class << obj
> =A0# now we are in obj's metaclass / eigenclass / singleton class
> end


I agree with this. For me they mean the same thing.

> I think the different names exist for historical reasons, I personally us=

e
> "metaclass" because that is what _why used in Dwemthy's Array
> http://mislav.uniqpath.com/poignant-guide/dwemthy/ but I think the name
> "eigenclass" sounds badass (reminiscent of steampunk), so maybe I'll star=

t
> using it, instead


I use "singleton class" .

Jesus.

 
Reply With Quote
 
Intransition
Guest
Posts: n/a
 
      12-02-2010


On Dec 2, 12:25=A0am, Tony Arcieri <tony.arci...@medioh.com> wrote:
> Every time I think I have my head around what these terms mean I seem to =

run
> across someone with a completely different definition.
>
> My understanding was that the singleton class is what you obtain when you
> call self.class in instance scope, and that metaclass and eigenclass
> are interchangeable terms for what you obtain if you call class << self;
> self; end in instance scope.
>
> Is this correct? Do you have a different definition?


Technically they are all the same.


> --
> Tony Arcieri
> Medioh! A Kudelski Brand


 
Reply With Quote
 
Gary Wright
Guest
Posts: n/a
 
      12-02-2010

On Dec 2, 2010, at 11:47 AM, Intransition wrote:
> On Dec 2, 12:25 am, Tony Arcieri <tony.arci...@medioh.com> wrote:
>> Every time I think I have my head around what these terms mean I seem =

to run
>> across someone with a completely different definition.
>>=20
>> My understanding was that the singleton class is what you obtain when =

you
>> call self.class in instance scope, and that metaclass and eigenclass
>> are interchangeable terms for what you obtain if you call class << =

self;
>> self; end in instance scope.
>>=20
>> Is this correct? Do you have a different definition?

>=20
> Technically they are all the same.


Perhaps it was the original question that was confusing but they
are not all the same. There are two different situations in the
examples given by the original poster:
=20
1) object.class in any context is the class of which 'object' is
an instance

self.class is just a particular example of this for 'self'

2) =20

Ruby 1.9.2:=20
object.singleton_class in any context is the (perhaps newly
created) singleton class for 'object'

self.singleton_class is again just a particular example
of this for 'self'

Prior to Ruby 1.9.2:
The expression, (class <<object; self; end), was needed to
access the singleton objects as there was no standard method
that returned a reference to the singleton class.

self.class and self.singleton_class are both classes but they
each play their own separate role in Ruby's method lookup
process.

There has been a long running debate in the Ruby community as to
what to name the object returned by (class <<object; self; end) but
with the introduction of Object#singleton_class in 1.9.2 that debate
is effectively over.

The most common alternative names were eigenclass, metaclass, and
perhaps virtual class as well as a long list of other variations.
Each one had its pros/cons but "singleton class" is the one that
Matz settled on in 1.9.2.

Gary Wright


 
Reply With Quote
 
Intransition
Guest
Posts: n/a
 
      12-02-2010


On Dec 2, 12:23=A0pm, Gary Wright <gwtm...@mac.com> wrote:
> On Dec 2, 2010, at 11:47 AM, Intransition wrote:
>
> > On Dec 2, 12:25 am, Tony Arcieri <tony.arci...@medioh.com> wrote:
> >> Every time I think I have my head around what these terms mean I seem =

to run
> >> across someone with a completely different definition.

>
> >> My understanding was that the singleton class is what you obtain when =

you
> >> call self.class in instance scope, and that metaclass and eigenclass
> >> are interchangeable terms for what you obtain if you call class << sel=

f;
> >> self; end in instance scope.

>
> >> Is this correct? Do you have a different definition?

>
> > Technically they are all the same.

>
> Perhaps it was the original question that was confusing but they
> are not all the same. =A0


The terms singleton class, eigenclass and metaclass are. Which I
believe was the question.


 
Reply With Quote
 
Gary Wright
Guest
Posts: n/a
 
      12-02-2010

On Dec 2, 2010, at 1:50 PM, Intransition wrote:

>=20
>=20
> On Dec 2, 12:23 pm, Gary Wright <gwtm...@mac.com> wrote:
>> On Dec 2, 2010, at 11:47 AM, Intransition wrote:
>>=20
>>> On Dec 2, 12:25 am, Tony Arcieri <tony.arci...@medioh.com> wrote:
>>>> Every time I think I have my head around what these terms mean I =

seem to run
>>>> across someone with a completely different definition.

>>=20
>>>> My understanding was that the singleton class is what you obtain =

when you
>>>> call self.class in instance scope, and that metaclass and =

eigenclass
>>>> are interchangeable terms for what you obtain if you call class << =

self;
>>>> self; end in instance scope.

>>=20
>>>> Is this correct? Do you have a different definition?

>>=20
>>> Technically they are all the same.

>>=20
>> Perhaps it was the original question that was confusing but they
>> are not all the same. =20

>=20
> The terms singleton class, eigenclass and metaclass are. Which I
> believe was the question.
>=20


As I said, the original question is somewhat vague. I read it as
asking if all of the following are the same:

self.class in instance scope =3D singleton class
metaclass =3D singleton class
eigenclass =3D singleton class

and that is not correct (i.e. self.class is not a singleton class).

Gary Wright=

 
Reply With Quote
 
Tony Arcieri
Guest
Posts: n/a
 
      12-02-2010
[Note: parts of this message were removed to make it a legal post.]

On Thu, Dec 2, 2010 at 11:50 AM, Intransition <> wrote:

> The terms singleton class, eigenclass and metaclass are. Which I
> believe was the question.
>


Yeah, thanks for the clarification everyone. I talked to a Ruby implementer
who agrees these three terms are interchangeable.

The confusion arose for me from "singleton methods", whose name comes from
things like rb_define_singleton_method() (in the C API) and the
singleton_method_callback.

I still see a lot of confusion as to what to call the object which
represents a class. "Singleton class" made sense to me in that context...
there is only one object, its class is Class, and as far as I can tell
singleton methods are instance-specific behaviors of this object.

So what is the proper term for the object (with "singleton methods") that
represents a class but is not its singleton class/metaclass/eigenclass?

How ****ing confusing...

--
Tony Arcieri
Medioh! A Kudelski Brand

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Singleton object vs. enhancing singleton class Paul McMahon Ruby 3 06-09-2008 06:05 AM
Singleton Modules rather than Singleton Classes Trans Ruby 12 09-14-2007 06:45 AM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments