: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?!
(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.
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.
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
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
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!
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.