13. #define ROBJECT_EMBED_LEN_MAX 3
struct RObject {
struct RBasic basic;
union {
struct {
long numiv;
VALUE *ivptr;
struct st_table *iv_index_tbl;
} heap;
VALUE ary[ROBJECT_EMBED_LEN_MAX];
} as;
};
This is a generic Object.
14. #define ROBJECT_EMBED_LEN_MAX 3
struct RObject {
struct RBasic basic;
union {
struct {
long numiv;
VALUE *ivptr;
struct st_table *iv_index_tbl;
} heap;
VALUE ary[ROBJECT_EMBED_LEN_MAX];
} as;
};
You can pretty much ignore this stuff.
15. #define ROBJECT_EMBED_LEN_MAX 3
struct RObject {
struct RBasic basic;
union {
struct {
long numiv;
VALUE *ivptr;
struct st_table *iv_index_tbl;
} heap;
VALUE ary[ROBJECT_EMBED_LEN_MAX];
} as;
};
Again, it has an RBasic representing its class (klass)
and internal attributes (flags).
16. #define ROBJECT_EMBED_LEN_MAX 3
struct RObject {
struct RBasic basic;
union {
struct {
long numiv;
VALUE *ivptr;
struct st_table *iv_index_tbl;
} heap;
VALUE ary[ROBJECT_EMBED_LEN_MAX];
} as;
};
It also has instance variables.
20. long numiv;
VALUE *ivptr;
struct st_table *iv_index_tbl;
st_table is a C hashtable implementation.
It’s also the underpinning for ruby hashes.
21. #define ROBJECT_EMBED_LEN_MAX 3
struct RObject {
struct RBasic basic;
union {
struct {
long numiv;
VALUE *ivptr;
struct st_table *iv_index_tbl;
} heap;
VALUE ary[ROBJECT_EMBED_LEN_MAX];
} as;
};
Back to the top
22. An Object has:
• klass (parent class)
• flags (frozen? tainted? etc.)
• Instance variables
• Nothing else.
29. struct RClass {
struct RBasic basic;
rb_classext_t *ptr;
struct st_table *m_tbl;
struct st_table *iv_index_tbl;
};
A class has attributes (flags) and a class (klass).
31. struct RClass {
struct RBasic basic;
rb_classext_t *ptr;
struct st_table *m_tbl;
struct st_table *iv_index_tbl;
};
m_tbl is a hash of methods. Think of it as:
{name -> method body}
32. struct RClass {
struct RBasic basic;
rb_classext_t *ptr;
struct st_table *m_tbl;
struct st_table *iv_index_tbl;
};
Just like iv_index_tbl on RObject, except the rest
of the ivar storage is done in rb_classext_t.
33. struct rb_classext_struct {
VALUE super;
struct st_table *iv_tbl;
struct st_table *const_tbl;
};
typedef struct rb_classext_struct
rb_classext_t;
This is the extended class information.
34. struct rb_classext_struct {
VALUE super;
struct st_table *iv_tbl;
struct st_table *const_tbl;
};
typedef struct rb_classext_struct
rb_classext_t;
‘super’ is a pointer to the class’s superclass.
35. struct rb_classext_struct {
VALUE super;
struct st_table *iv_tbl;
struct st_table *const_tbl;
};
typedef struct rb_classext_struct
rb_classext_t;
iv_tbl is a hash of {ivar name -> ivar value}
36. struct rb_classext_struct {
VALUE super;
struct st_table *iv_tbl;
struct st_table *const_tbl;
};
typedef struct rb_classext_struct
rb_classext_t;
similarly, const_tbl stores constants as
{const name -> const value}
37. struct RClass {
VALUE flags; // attributes
VALUE klass; // parent class (often Class)
VALUE super; // superclass (often Object)
struct st_table *iv_tbl; // ivars
struct st_table *const_tbl; // constants
struct st_table *m_tbl; // methods
struct st_table *iv_index_tbl; // ivars
};
An incorrect but helpful simplification of RClass.
70. Changes self? Changes definee?
class C C C
C.class_eval C C
C.instance_eval C C.singleton_class
obj.instance_eval obj obj.singleton_class
(in C) def foo obj obj.singleton_class
obj.send :eval obj NO CHANGE
class << obj obj.singleton_class obj.singleton_class