Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Ruby Object Model - Medellin.rb

The Ruby Object Model - Medellin.rb

An overview of the Ruby Object model

Avatar for Alejandro Cadavid

Alejandro Cadavid

February 24, 2016

Other Decks in Programming

Transcript

  1. Contents 1. Motivation 2. Setup 3. Object dissection 4. Class

    dissection 5. Eigenclass 6. Method lookup
  2. Motivation 1. What’s happening when we invoke something like has_many

    :articles in Rails? 2. What’s happening when we call puts “Hi” in the IRB console? 3. What’s happening when we call a method on an object but you can’t find its definition in the object’s class?!
  3. Setup class Car def start_engine puts “engine started!” end end

    m = Car.new m.start_engine # => “engine started”
  4. An object dissection m m = Car.new class Car //…

    (methods) end @color = “Green” @brand = “Renault” Note: Instance variables exist independently of the class to which the object belongs to. They only appear when assigned.
  5. An object implementation RObject RBasic basic numiv ivptr iv_index_tbl RBasic

    VALUE flags VALUE klass • numiv -> Number of instance variables • ivptr -> Array of instance variables values • iv_index_tbl -> Pointer to hash table in RClass
 that matches values and names for instance vars. • flags -> Boolean technical values • ivptr -> Pointer to the class the object
 is instance of *For basic objects like Strings, Arrays, etc there are RString, RArray, and so on for optimization reasons. For more simple objects (ints, etc) the value itself is stored in the VALUE pointer and a HT is used to get instance vars.
  6. A class dissection c = Car c class Class //…

    (methods*) end list of instance methods (ex. start_engine) c.superclass => Module *Class methods are actually attached to a singleton class!
  7. A class implementation RClass RBasic basic m_tbl iv_index_tbl super iv_tbl

    refined_class (…) • m_tbl -> Pointer to methods table • iv_index_tbl -> Pointer to HS that matches instance
 variables names and their values • super -> Pointer to super class • iv_tbl -> Pointer to HT with class-instance and class 
 variables values. • refined_class -> Refinements pointer
  8. Eigenclass (Singleton) m // Let’s imagine this actually exists class

    Anonymous // (stop method) + // Reference to Car class end class Car do //… (methods) end m = Car.new def m.stop do puts “stopped!” end m = Car.new class << m def stop do puts “stopped!” end end
  9. A method’s long road to execution m m = Car.new

    m.throttle Help! Help me, method lookup! Wait! Who is self ? 1. Eigenclass 2. Object’s class 3. Parent objects (ancestor chains and included modules) 4. method_missing Pro-tip: Use obj.method(:my_method).source_location to find where a method is defined!
  10. Puts in console 2.3.0 :001 > puts "Hi" Hi =>

    nil 2.3.0 :002 > self => main 2.3.0 :003 > self.class => Object 2.3.0 :004 > self.send(:puts, "Hi") Hi => nil Who is self?
  11. Modules mixing m class Anonymous // References MyModule Methods end

    class MyClassWithInclude do //… (methods) end class MyClassWithInclude include MyModule end
  12. Conclusion 1. Objects are really the core of Ruby. They

    may same like magic but they are actually simple entities. 2. Method lookup is a very powerful tool that you can take advantage of to be more expressive. 3. Don’t fear the internals! They hold the key to deep Ruby understanding.
  13. References 1. Programming Ruby Chapter 24. Dave Thomas, Andy Hunt,

    Chad Fowler. 2. Metaprogramming Ruby Chapter 2,3,5. Paolo Perrota. 3. Ruby Under a Microscope Chapter 5. Pat Shaughnessy.