torsdag, maj 29, 2008

Ruby doesn't have meta classes

OK. It's time to get rid of this terminology problem. Ruby does NOT have meta classes. You can define them yourself, but it's not the same thing as what is commonly called the meta class. That is more correctly called the eigen class. The singleton class is also better than meta class, but eigen class is definitely the most correct term.

So what is a meta class then? Well, it's a class that defines the behavior of other classes. You can define meta classes in Ruby if you want too by defining a subclass of Class. Those classes would be metaclasses.

Edit: Of course, if you actually try to define a subclass of Class you will find that Ruby doesn't allow you to do that, which means that you don't have any meta classes in Ruby. Period.

9 kommentarer:

mernen sa...
Den här kommentaren har tagits bort av skribenten.
mernen sa...

The use of "metaclass" is quite confusing indeed. Normally I call them singleton classes, since that's what most impls seem to use internally, but eigenclass is good too.

> You can define meta classes in Ruby if you want too by defining a subclass of Class.

Isn't that blocked? I mean, with a few hoops it's possible to allocate a subclass and even initialize it, but still it won't be instantiable.

From my understanding, Class is the metaclass in Ruby, and necessarily the only one. Or is there something I'm missing in the concept of metaclasses that require allowing you to define new metaclasses?

(Anyway, point taken and agreed: calling class << self the metaclass is erroneous and at times confusing)

Paul Barry sa...

Where are the terms meta class and eigen class defined?

Anonym sa...

Well... technically that is subjective still since an eigenclass can fill the role of a metaclass. That _does not_ make all eigenclasses metaclasses at all and it is an important mistake many make in their terminology (not accusing you of this).

So when is an eigenclass a metaclass? Like you said, one would have to subclass Class to create a metaclass. works in some implementations (MRI) but is still useless since it will fail to allocate objects from that. So how does one have a class that points back to Class as a superclass?

I am sure many already know the answer but to fill in the blanks we should inspect an eigenclass that fills the role of the so called metaclas:

meta = class << String; self end
meta.ancestors #=> [Class, Module, Object ...]

As we can see here, the eigenclass we extract here has ancestors that include Class which fulfills the role of metaclasses in other languages (providing things like class methods -- not the same as static methods).

Finally, in some ways, Class itself is a metaclass (as it is considered so in Smalltalk)... but this is more of a boring detail.

Reginald Braithwaite sa...

Thank you. I vastly prefer the term “eigenclass” to the alternatives “metaclass” or “singleton class.”

Eigenclasses are entirely unrelated to metaclasses in other OO languages like Smalltalk, so I find that confusing.

lrz sa...

$ ruby -ve "p"
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
$ macruby -ve "p"
MacRuby version 0.2 (ruby 1.9.0 2008-05-17) [universal-darwin9.0]

mernen sa...

Brian, indeed, eigenclasses of classes are also technically metaclasses. Didn't think of that when writing my reply.

lrz: now try :)

patrick farley sa...


Nail on the head. Eigenclasses of classes are Metaclasses. Their reason for existence is to define the behavior of another class. It's true that Ruby only allows one instance of them to exist (the one assigned to the class name constant), but I don't see how that makes them any less of a Metaclass. Certainly using the term Metaclass for classes defined to provide instance specific behavior to plain 'ol objects is a bad idea. I wrote up my own little glossary a little while back to suss out the terminology for myself.

Anonym sa...

看房子,買房子,建商自售,自售,台北新成屋,台北豪宅,新成屋,豪宅,美髮儀器,美髮,儀器,髮型,EMBA,MBA,學位,EMBA,專業認證,認證課程,博士學位,DBA,PHD,在職進修,碩士學位,推廣教育,DBA,進修課程,碩士學位,網路廣告,關鍵字廣告,關鍵字,課程介紹,學分班,文憑,牛樟芝,段木,牛樟菇,日式料理, 台北居酒屋,日本料理,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,台北結婚,場地,住宿,訂房,HOTEL,飯店,造型系列,學位,牛樟芝,腦磷脂,磷脂絲胺酸,SEO,婚宴,捷運,學區,美髮,儀器,髮型,牛樟芝,腦磷脂,磷脂絲胺酸,看房子,買房子,建商自售,自售,房子,捷運,學區,台北新成屋,台北豪宅,新成屋,豪宅,學位,碩士學位,進修,在職進修, 課程,教育,學位,證照,mba,文憑,學分班,網路廣告,關鍵字廣告,關鍵字,SEO,关键词,网络广告,关键词广告,SEO,关键词,网络广告,关键词广告,SEO,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北結婚,場地,結婚,場地,推車飲茶,港式點心,尾牙春酒,台北結婚,婚宴場地,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北結婚,場地,居酒屋,燒烤,美髮,儀器,髮型,美髮,儀器,髮型,美髮,儀器,髮型,美髮,儀器,髮型,小套房,小套房,進修,在職進修,留學,證照,MBA,EMBA,留學,MBA,EMBA,留學,進修,在職進修,牛樟芝,段木,牛樟菇,關鍵字排名,網路行銷,关键词排名,网络营销,網路行銷,關鍵字排名,关键词排名,网络营销,PMP,在職專班,研究所在職專班,碩士在職專班,PMP,證照,在職專班,研究所在職專班,碩士在職專班,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,EMBA,MBA,PMP