Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ruby Internals

7,065 views

Published on

A brief tour of ruby's object model and browsing the C source of MRI. This is only about a third of the workshop I gave.

Published in: Technology, Business
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download Full EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download Full EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download doc Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download Full EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download doc Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • it's great! thanks
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Ruby Internals

  1. 1. RUBY INTERNALS Use the source, Luke! TW: @burkelibbey GH: @burke
  2. 2. RUBY INTERNALS Use the source, Luke! TW: @burkelibbey GH: @burke
  3. 3. TOPICS• Basic object structure• Class inheritance• Singleton classes• Module inheritance• Contexts
  4. 4. BASIC OBJECT STRUCTURE
  5. 5. struct RBasic {     VALUE flags;     VALUE klass; };Every object in ruby has an instance of RBasic.
  6. 6. struct RBasic {     VALUE flags;     VALUE klass; };flags stores information like whether the object is frozen, or tainted, or others.
  7. 7. struct RBasic {     VALUE flags;     VALUE klass; };klass is a pointer to the parent class (or singleton class)
  8. 8. typedef uintptr_t VALUE;VALUE is used like a void pointer in ruby C code.
  9. 9. struct RFloat {    struct RBasic basic;    double float_value;}; This is a float.
  10. 10. struct RFloat {     struct RBasic basic;     double float_value; };Like everything else, it has an RBasic.
  11. 11. struct RFloat {     struct RBasic basic;     double float_value; };...and also the actual floating point value.
  12. 12. brb c
  13. 13. #define ROBJECT_EMBED_LEN_MAX 3struct 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. 14. #define ROBJECT_EMBED_LEN_MAX 3struct 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. 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. 16. #define ROBJECT_EMBED_LEN_MAX 3struct 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.
  17. 17.       long numiv;      VALUE *ivptr;      struct st_table *iv_index_tbl; ivptr points to an array of ivar values.
  18. 18.       long numiv;      VALUE *ivptr;      struct st_table *iv_index_tbl;Unsurprisingly, numiv is the number of ivars.
  19. 19.       long numiv;      VALUE *ivptr;      struct st_table *iv_index_tbl; iv_index_tbl is essentially a hash of {name -> index into ivptr}
  20. 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. 21. #define ROBJECT_EMBED_LEN_MAX 3struct 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. 22. An Object has: • klass (parent class) • flags (frozen? tainted? etc.) • Instance variables • Nothing else.
  23. 23. brb c
  24. 24. We saw that Float has a distinct implementation from Object
  25. 25. String, Regexp, Array, Hash, File, Rational, Complex, Data, and Bignum do too. These are mostly for performance reasons.
  26. 26. brb c
  27. 27. Class is the other exception.Not just for performance. It has a lot of extra behaviour.
  28. 28. struct RClass {    struct RBasic basic;    rb_classext_t *ptr;    struct st_table *m_tbl;    struct st_table *iv_index_tbl;};
  29. 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).
  30. 30. struct RClass {     struct RBasic basic;     rb_classext_t *ptr;     struct st_table *m_tbl;     struct st_table *iv_index_tbl; };rb_classext_t stores more class-specific info
  31. 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. 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. 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. 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. 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. 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. 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.
  38. 38. brb c
  39. 39. CLASS INHERITANCE
  40. 40. Let’s look at an example class and its RClass
  41. 41. class Oban < Scotch  AGE = 14  @tasty = true  def tasty    Oban.instance_variable_get("@tasty")  endend
  42. 42. class Oban < Scotch  AGE = 14  @tasty = true  def tasty    Oban.instance_variable_get("@tasty")  endend basic.klass Class ptr->super Scotch iv_tbl {“@tasty” => true} const_tbl {“AGE” => 14} m_tbl {“tasty” => ...}
  43. 43. Another: class Animal ; endclass Dog < Animal ; end
  44. 44. Let’s look at another class:
  45. 45. class Scotch < Liquor  def tasty?    true  endend
  46. 46. class Scotch < Liquor   def tasty?     true   end endThis lets us call: Scotch.new.tasty?
  47. 47. class Scotch < Liquor   def tasty?     true   end endThis lets us call: Scotch.new.tasty?And puts {“tasty?” -> ...} into the m_tbl
  48. 48. class Scotch < Liquor   def tasty?     true   end endWhat if we wanted ‘tasty?’ to be a class method?
  49. 49. It clearly works, but how does ruby know it’s a class method? class Scotch < Liquor   def self.tasty?     true   end end There’s only one m_tbl.
  50. 50. SINGLETON CLASSES
  51. 51. When you define a method with“def self.(name)”, you create a singleton class.
  52. 52. BASIC OBJECT STRUCTURE
  53. 53. BASIC OBJECT STRUCTURE
  54. 54. ‘self ’ is not everything it appears to be.
  55. 55. class Foo   # self == Foo   def bar   end end‘bar’ is defined as an instance method of ‘Foo’
  56. 56. class Foo   def bar   end   def self.bar   end endThese should be the same, right?
  57. 57. class Foo   def bar   end   def self.bar   end end...because these are the same: my_method self.my_method
  58. 58. class Foo   def bar   end   def self.bar   end endInvocations use ‘self ’ if no receiver is given. Don’t definitions? O_o
  59. 59. NOPE.
  60. 60. def foo Defines on the default defineedef target.bar Defines on target.singleton_class
  61. 61. ie: There’s a second, hidden context. default_definee
  62. 62. default_definee Target for method definitions with no target self Receiver for method invocations with no receiver
  63. 63. No easy way to reference the default definee.
  64. 64. eval "def _d;end"y = method(:_d).owner rescue instance_method(:_d).ownereval "undef _d"y This is ugly, but it works.
  65. 65. DEFINEE = eval "def _d;end";y = method(:_d).owner rescueinstance_method(:_d).owner;eval "undef _d";yclass Foo  puts eval DEFINEE #=> Fooend Really ugly. Really works.
  66. 66. self != default definee
  67. 67. Changes self? Changes definee? class C C C C.class_eval C CC.instance_eval C C.singleton_classobj.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

×