Glossary
Definitions of terms and phrases used in the Ruby programming language and in this implementation. See also “The Ruby Programming Language” by Flanagan and Matsumoto [O’Reilly 2008] and “Programming Ruby: The Pragmatic Programmer’s Guide” 2nd or 3rd Edition by Thomas et al [The Pragmatic Programmers 2005-2008]
-
metaclass
Also called the +singleton+ class or +eigenclass+. Every object in Ruby can have one, although, they are only created as necessary. The metaclass holds the method and constant tables that belong only to a particular object instance. For example, the method +hello+ defined below exists only in the metaclass for +obj+.
obj = Object.new def obj.hello puts 'hi' end
Since all classes in Ruby are also objects, they can have metaclasses. The methods called “class methods” are just methods in the method_table of the class’s metaclass. The method +honk+ exists in the metaclass for the class +Car+.
class Car def self.honk end end
In Rubinius, metaclasses are all instances of the class MetaClass. The metaclass for an object can be obtained by calling the +metaclass+ method. The overall arrangement of concepts involved here is sometimes referred to as the ‘Meta-Object Protocol’ or +MOP+.
-
method lookup or method resolution
The rule is simple: Take the object located in the class slot of the object (which is not always the return value of Object#class) and begin searching.
Searching goes up the superclass chain until the superclass is nil.
In which case, redo lookup for method_missing. If we fail to find method_missing, fail tragicly.
+-------------+ | nil | +-------------+ ^ | superclass | +-------------+ | Object | +-------------+ ^ | superclass | +-------------+ | Module | +-------------+ ^ | superclass | +-------------+ | Class | +-------------+ ^ | superclass | +-------------+ | MetaClass | | (Object) | +-------------+ ^ | superclass | +-------------+ +-------------+ | F | -----------------> | MetaClass | +-------------+ metaclass | (F) | +-------------+ class Class def wanker puts 'you are' end end class F def self.bloke wanker end end
-
Resolve method ‘wanker’ – search method_tables in:
- MetaClass(F)
- MetaClass(Object)
- Class
Found
-
-
method_table
A data structure in every class (and module) that contains the methods defined for that class.
In Rubinius, a class’s method_table is an instance of LookupTable.
-
MatzRuby
See MRI
-
MRI
Matz’s Ruby Interpreter or Matz’s Ruby Implementation. A short name to refer to the official implementation of Ruby. See http://ruby-lang.org.
-
private send
A method call that has no explicit lexical receiver. The receiver of the call is +self+. For example:
class A private def you_are_mine end end class B < A def sunshine you_are_mine end end class C def dear today = B.new today.you_are_mine end end
The call to +you_are_mine+ in the method +sunshine+ is a private send. The call to +today.you_are_mine+ will not succeed because private methods cannot have an explicit receiver. In this case, the object +today+ is the explicit receiver.
-
superclass
The class that a particular class immediately inherits from. The class Object is the superclass of all classes that do not inherit explicitly from a class.
class A end class B < A end
Class A inherits from Object. In other words, A.superclass == Object. Class B inherits explicitly from class A. So, B.superclass == A.