Class: Object

Inherits:
BasicObject
Defined in:
lib/internal/classtree.rb,
lib/internal/object/as_code.rb

Constant Summary collapse

RubyVM =
rb_const_get(rb_cObject, rb_intern("VM"))

Instance Method Summary collapse

Instance Method Details

#as_code(indent = 0) ⇒ Object



5
6
7
8
# File 'lib/internal/object/as_code.rb', line 5

def as_code(indent=0)
  # TODO: this won't work for many objects
  "#{'  '*indent}#{self.inspect}"
end

#classtree(s = '', prefix = '', obj = self, graphed = {}) ⇒ Object

Returns an ASCII representation of a class hierarchy, e.g.:

irb(main):004:0> puts Object.new.classtree
#<Object:0x40330ce8>
+-class = Object
  |-class = #<Class:Object>
  | |-class = Class
  | | |-class = #<Class:Class>
  | | | |-class = #<Class:Class> (*)
  | | | +-super = #<Class:Module>
  | | |   |-class = Class (*)
  | | |   +-super = #<Class:Object> (*)
  | | +-super = Module
  | |   |-class = #<Class:Module> (*)
  | |   +-super = Object (*)
  | +-super = Class (*)
  +-super = #<PP::ObjectMixin?:0x40349568>
    +-class = PP::ObjectMixin?
      |-class = Module (*)
      +-super = #<Kernel:0x4033507c>
        +-class = Kernel
=> nil

An asterisk (*) indicates that the class has previously been printed.

The notation #<Class:XXXX> indicates a singleton class.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/internal/classtree.rb', line 33

def classtree(s = '', prefix = '', obj=self, graphed={})
  # if Module == obj.class.class then
  #   s << "included module "
  # elsif obj.singleton? then
  #   s << "singleton class "
  # elsif Class === obj then
  #   s << "class "
  # end
  s << "#{obj}"
  if graphed[obj] then
    s << " (*)\n"
    return s
  end
  s << "\n"
  graphed[obj] = true
  return if Kernel == obj
  subtree = (Module === obj) && (obj.real_superclass)
  s << "#{prefix}#{subtree ? '|-' : '+-'}class = "
  classtree(s, prefix + (subtree ? '| ' : '  '), obj.real_class, graphed)
  if subtree then
    s << "#{prefix}+-super = "
    classtree(s, prefix + '  ', obj.real_superclass, graphed)
  end
  return s
end

#has_singleton?Boolean

Return true if this object has a singleton class.

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'ext/internal/object/object.c', line 40

static VALUE has_singleton(VALUE self)
{
  if(FIXNUM_P(self) || SYMBOL_P(self))
  {
    return Qfalse;
  }
  else if(rb_special_const_p(self))
  {
    return Qtrue;
  }
  else
  {
    return FL_TEST(RBASIC(self)->klass, FL_SINGLETON) ? Qtrue : Qfalse;
  }
}

#real_classClass

Return the object’s first immediate ancestor; this may be the object’s class, its singleton class, or a module singleton.

Returns:

  • (Class)


10
11
12
13
14
15
16
17
18
19
20
# File 'ext/internal/object/object.c', line 10

static VALUE real_class(VALUE self)
{
  if(IMMEDIATE_P(self))
  {
    return CLASS_OF(self);
  }
  else
  {
    return RBASIC(self)->klass;
  }
}

#singleton?Boolean

Return true if this object is a singleton (that is, it has the FL_SINGLETON flag set).

Returns:

  • (Boolean)


29
30
31
32
# File 'ext/internal/object/object.c', line 29

static VALUE is_singleton(VALUE self)
{
  return FL_TEST(self, FL_SINGLETON) ? Qtrue : Qfalse;
}

#singleton_classClass

Return the object’s singleton class. Creats a new singleton class for the object if it does not have one. See RCR#231.

Returns:

  • (Class)


63
64
65
66
# File 'ext/internal/object/object.c', line 63

static VALUE singleton_class(VALUE self)
{
  return rb_singleton_class(self);
}