Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Metaclass
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==In Ruby== Ruby purifies the Smalltalk-80 concept of metaclasses by introducing [[wiktionary:eigenclasses|eigenclasses]], removing the <code>Metaclass</code> class, and (un)redefining the class-of map. <!-- --> The change can be schematized as follows:<ref>{{cite web | url=http://www.atalon.cz/rb-om/ruby-object-model/co-smalltalk/ | title=The Ruby Object Model: Comparison with Smalltalk-80 }}</ref> {| border=0 cellpadding=0 style="margin: 2ex auto" |- | {| class="wikitable" |+ Smalltalk-80 |- | <div style="height: 5ex; margin:1ex; padding: 1ex; border: 1px dotted gray;"><span style="position:relative; top:1ex;">Classes</span></div> | <div style="height: 5ex; margin:1ex; padding: 1ex 0.4ex; border: 1px dotted gray;"><span style="font-size:85%;">'''Implicit<br>metaclasses'''</span></div> | rowspan="2" style="width:4ex;" | |- | <div style="height: 5ex; margin:1ex; padding: 1ex; border: 1px dotted gray;">Terminal<br>objects</div> | |} | style="font-size: 200%; vertical-align: middle; padding: 1px 20px;" | → | {| class="wikitable" |+ Ruby |- | <div style="height: 5ex; margin:1ex; padding: 1ex; border: 1px dotted gray;"><span style="position:relative; top:1ex;">Classes</span></div> | <div style="height: 5ex; margin:1ex; padding: 1ex 0.4ex; border: 1px dotted gray;"><span style="font-size:85%;">'''Eigenclasses of<br>classes'''</span></div> | rowspan=2 | <div style="height: 12ex; margin:1ex; padding: 2ex 0.4ex; border: 1px dotted gray;"><span style="font-size:85%;">'''Eigenclasses<br>of<br>eigenclasses'''</span></div> |- | <div style="height: 5ex; margin:1ex; padding: 1ex; border: 1px dotted gray;">Terminal<br>objects</div> | <div style="height: 5ex; margin:1ex; padding: 1ex 0.4ex; border: 1px dotted gray;"><span style="font-size:85%;">'''Eigenclasses of<br>terminal objects'''</span></div> |} |} Note in particular the correspondence between Smalltalk's implicit metaclasses and Ruby's eigenclasses of classes. The Ruby eigenclass model makes the concept of implicit metaclasses fully uniform: every object ''x'' has its own meta-object, called the ''eigenclass'' of ''x'', which is one meta-level higher than ''x''. The "higher order" eigenclasses usually exist purely conceptually – they do not contain any methods or store any (other) data in most Ruby programs.<ref> {{cite book | publisher=Pragmatic Bookshelf | author=Paolo Perrotta | url=http://pragprog.com/book/ppmetr/metaprogramming-ruby | title=Metaprogramming Ruby | year=2010 | isbn=978-1-934356-47-0}}</ref> The following diagrams show a sample core structure of Smalltalk-80 and Ruby in comparison.<ref>{{cite web | url=http://www.atalon.cz/om/object-membership/ | title=Object Membership: The Core Structure of Object Technology }}</ref> In both languages, the structure consists of a built-in part which contains the circular objects (i.e. objects that appear in a cycle formed by a combination of blue or green links) and a user-part which has four explicit objects: classes <code>A</code> and <code>B</code> and terminal objects <code>u</code> and <code>v</code>. Green links show the child→parent relation of inheritance (with the implicit upward direction), blue links show the complementary member→container relation of instantiation (a blue link from ''x'' points to the least actual container of ''x'' that is the start point for the method lookup when a method is invoked on ''x''). Gray nodes display the eigenclasses (resp. implicit metaclasses in the case of Smalltalk-80). <!-- (<code>A</code> is a direct subclass of <code>Object</code>, <code>B</code> is a direct subclass of <code>A</code> and both <code>u</code> and <code>v</code> are direct instances of <code>B</code>). --> {| border=0 cellpadding=0 cellspacing=0 style="margin: 2ex auto" |- | style="text-align:center" | Smalltalk-80|| style="width:4ex" | || style="text-align:center" |Ruby |- | [[File:Smalltalk-metaclass-sample.svg|300px|Implicit metaclasses in Smalltalk-80 - A sample structure]]|| ||[[File:Ruby-metaclass-sample.svg|500px|Eigenclasses in Ruby - A sample structure]] |} The diagram on the right also provides a picture of [[lazy evaluation]] of eigenclasses in Ruby. The <code>v</code> object can have its eigenclass evaluated (allocated) as a consequence of adding ''singleton methods'' to <code>v</code>. According to the Ruby's introspection method named <code>class</code>, the class of every class (and of every eigenclass) is constantly the <code>Class</code> class (denoted by <code>c</code> in the diagram). <code>Class</code>, and <code>Struct</code> are the only classes that have classes as instances.<ref>{{cite web|title=Struct|url=https://docs.ruby-lang.org/en/master/Struct.html}}</ref> {{Disputed inline|The_Struct_class_in_Ruby|date=May 2015}} Subclassing of <code>Class</code> is disallowed. <!-- --> Following the standard definition of metaclasses we can conclude that <code>Class</code> and <code>Struct</code> are the only metaclasses in Ruby. This seems to contradict the correspondence between Ruby and Smalltalk, since in Smalltalk-80, every class has its own metaclass. The discrepancy is based on the disagreement between the <code>class</code> introspection method in Ruby and Smalltalk. While the map ''x ↦ x.''<code>class</code> coincides on terminal objects, it differs in the restriction to classes. As already mentioned above, for a class <code>''x''</code>, the Ruby expression <code>''x''.class</code> evaluates constantly to <code>Class</code>. In Smalltalk-80, if <code>''x''</code> is a class then the expression <code>''x'' class</code> corresponds to the Ruby's <code>''x''.singleton_class</code> – which evaluates to the eigenclass of <code>''x''</code>.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)