Sketching Ruby object model using common sense

(...and IRB...and google... and MRI source)

  • values in Ruby are objects ~ intewebs

  • ...even classes...

  • objects "mutability" ~ Ruby "metability"

  • internal representatinon:

struct RBasic {
    VALUE flags;
    const VALUE klass;
};
struct RObject {
    struct RBasic basic;
    long numiv;
    VALUE *ivptr;
    // ...
};
struct RClass {
    struct RBasic basic;
    VALUE super;
    rb_classext_t *ptr;
    struct method_table_wrapper *m_tbl_wrapper;
};

Object:

  • class pointer
  • array of instance variables 

Class: 

  • object
  • method definitions
  • super pointer

Questions:

  1. Method definition table in class can contain only one "kind" of methods, instance methods, where are class methods?
  2. Class methods or Ruby lookup in different place?
  3. Are class methods language feature or is there a way to "describe those class objects"(meta)?
*** object DON'T have methods, classes DO ***

Anonymous/Einingen/Meta/Singleton/Virtual

class Object
  def my_describing_class
    class << self; self; end
  end
end

$> Class.singleton_class
 => #<Class:Class>
  • offten called hidden, but we have a method specifically for them???

$> irb
$> ObjectSpace.count_objects[:T_CLASS]
 => 961
$> class User; end; ObjectSpace.count_objects[:T_CLASS]
 => 963
VALUE rb_class_real(VALUE cl)
{
  while (cl && ((RBASIC(cl)->flags & FL_SINGLETON))
    cl = RCLASS_SUPER(cl);
  return cl;
}

VALUE rb_obj_class(VALUE obj)
{
  return rb_class_real(CLASS_OF(obj));
}
Made with Slides.com