1 # depends on: object.rb
4 # Classes in Ruby are first-class objects, each is an instance of
7 # When a new class is created (typically using <tt>class Name; ... end</tt>),
8 # an object of type Class is created and assigned to a global constant (Name
9 # in this case). When <tt>Name.new</tt> is called to create a new object, the
10 # new method in Class is run by default.
12 # This can be demonstrated by overriding new in Class:
17 # puts "Creating a new #{self.name}"
31 # Classes, modules, and objects are interrelated. In the diagram that follows,
32 # the vertical arrows represent inheritance, and the parentheses meta-classes.
33 # All metaclasses are instances of the class Class.
35 # +------------------+
37 # Object---->(Object) |
40 # | | +-----+ +---------+ |
44 # +------+ | Module--->(Module) |
46 # OtherClass-->(OtherClass) | | |
55 protected :object_type
57 protected :needs_cleanup
59 def initialize(sclass=Object)
60 unless sclass.kind_of?(Class)
61 raise TypeError, "superclass must be a Class (#{sclass.class} given)"
64 @instance_fields = sclass.instance_fields
65 @has_ivar = sclass.has_ivars
66 @needs_cleanup = sclass.needs_cleanup
67 @object_type = sclass.object_type
73 mc.set_superclass sclass.metaclass
76 class_eval(&block) if block
77 # add class to sclass's subclass list, for ObjectSpace.each_object(Class)
78 # NOTE: This is non-standard; Ruby does not normally track subclasses
79 sclass.__send__ :inherited, self
82 def opened_class_cv(cls)
83 cls = Object unless cls
84 cls.add_subclass(self)
86 cls.send :inherited, self
88 self # FIXME: hook calls should preserve the stack
93 # NOTE: This method is not standard Ruby; JRuby implements it, but not
97 def add_subclass_cv(cls)
104 # NOTE: This method is not standard Ruby; JRuby implements it, but not
108 def subclasses_cv(descend = false)
118 def subclasses_descend(all = [])
119 return unless @subclasses
120 @subclasses.each {|cls| all << cls; cls.subclasses_descend(all)}
125 # Returns the Class object that this Class inherits from. Included Modules
126 # are not considered for this purpose.
129 cls = direct_superclass
130 return nil unless cls
131 while cls and cls.kind_of? IncludedModule
132 cls = cls.direct_superclass
140 def self.after_loaded
141 alias_method :opened_class, :opened_class_cv
142 alias_method :add_subclass, :add_subclass_cv
143 alias_method :__subclasses__, :subclasses_cv
149 ivar_as_index :superclass => 6
152 # Called when <tt>def obj.name</tt> syntax is used in userland
154 def attach_method(name, object)
155 # All userland added methods start out with a serial of 1.
158 cur = method_table[name]
159 if cur and cur.kind_of? Tuple
160 # Override the slot which points to the method, so that we
164 method_table[name] = Tuple[:public, object]
167 object.inherit_scope MethodContext.current.sender.method
168 Rubinius::VM.reset_method_cache(name)
170 # Call singleton_method_added on the object in question. There is
171 # a default version in Kernel which does nothing, so we can always
173 attached_instance.__send__ :singleton_method_added, name
178 def set_superclass(obj)
183 "#<MetaClass #{attached_instance.inspect}>"