deciphering the ruby object model
DESCRIPTION
My talk at RubyConf India 2011 at Bangalore on the 28th of May. I had received good feedback on this talk and folks wanted the material to be shared. So here it goes...FYI - The resources slide is one that you should not neglect at all.TRANSCRIPT
![Page 1: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/1.jpg)
Deciphering the Ruby Object Model
![Page 2: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/2.jpg)
Who am I?
• Karthik Sirasanagandla• A Pragmatic Programmer @ ThoughtWorks• VB freelance programmer for about a year• Java/JEE apps. developer for about 5 years• Ruby Enthusiast for over 8 months now• Haskell newbie• Occasionally blog at kartzontech.blogspot.com• Not so frequently tweet using @kartzontech
![Page 3: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/3.jpg)
Why Learn Ruby Object Model?
• Good if you know “syntax and semantics”
• Better if you know “Ruby’s Object Model”
• “A language the does not affect the way you think about programming is not worth learning at all” – Alan Perlis
![Page 4: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/4.jpg)
The Axiom
Kernel
AnyOtherClass
BasicObject
Object
Module
Class
Superclass
Superclass
Superclass
Superclass
Class
Class
You don’t question an axiom,rather start with it…
![Page 5: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/5.jpg)
Classes
Classes should be closed for modification and open to extension
class MyClass
end
class MyClass
def im # instance method
puts “Instance method”
end
def self.cm #class method
puts “Class method”
end
end
![Page 6: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/6.jpg)
Classes
“With more power comes more responsibilities” opines Matz,
like uncle Ben in the spiderman movie
Open Classes ??? Monkey Patching ???
![Page 7: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/7.jpg)
Classes
Class methods can be defined in number of ways:
class MyClass
def MyClass.cm1
puts “cm1() invoked…”
end
def self.cm2
puts “cm2() invoked…”
end
class << self
def cm3
puts “cm3() invoked…”
end
end
end
![Page 8: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/8.jpg)
Classes
Classes are objects
class MyClass
def im
puts “Instance method”
end
end
is the same as:
MyClass = Class.new do
def im
puts “Class method”
end
end
![Page 9: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/9.jpg)
Classes
Quiz Time:
class MyClass < String.new
def self.to_s
puts “Class method”
end
end
MyClass.to_s
What is the output? TypeError: Wrong argument type String (expected Class)
Lesson:Classes inherit from classes only not from just any object
![Page 10: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/10.jpg)
ClassesNo connection between a class and its objects’ instance variables
my_obj1______________________
@var_1 = 1@var_2 = “Ruby Conf”
my_obj2______________________
@v = 123
MyClass______________________
my_instance_method()
my_obj3______________________
ClassClassClass
![Page 11: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/11.jpg)
Classes
Quiz:
What is the Java/C# Interface equivalent in Ruby? [FYI: This has nothing to do with what I have been talking about all through!!]
Answer:
Interfaces are irrelevant in Ruby.But why? Duck Typing is the answer.
Program to an interfaceNo need to inherit from a common interface.
![Page 12: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/12.jpg)
The joy of “self” discovery
• self is not the same as this in Java
• self is synonymous to current/default object
• Who gets to be self depends on Where self is
![Page 13: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/13.jpg)
The joy of “self” discoveryCode is an unbeatable teaching aid. What say?
p “At Topmost level, self is #{self}”
class MyClass
p “In class definition block, self is #{self}”
def self.my_class_method
p “In my_class_method, self is #{self}”
end
def my_instance_method
p “In my_instance_method, self is #{self}”
end
end
![Page 14: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/14.jpg)
Class, Instance and Class Instance VariablesExperience is the best teacher.
class A
y = 1
@p = 2
@q
@@t = 3
def initialize
@@r ||= 4
@s = 5
end
end
puts “Class instance variables of A are #{A.instance_variables}”
[:@p]
puts “Class variables of A are #{A.class_variables}”
[:@@t]
a = A.new
puts “a, of type A, has instance variables #{a.instance_variables}”
[:@s]
puts “Class variables of A are #{A.class_variables}”
[:@@t, :@@r]
![Page 15: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/15.jpg)
Access modifiers• Ruby has 3 access modifiers for methods:
– Public (default)– Private– Protected
• Access modifiers apply until the end of scope, or until another access modifier pops-up
class MyClass
#public is the default access modifierdef m1; end
privatedef m2; end;def m3; end;
protecteddef m4; end;
end
![Page 16: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/16.jpg)
Access modifiers• private, public, protected are all
methods, so you can pass method name as symbol to it and change its visibility
class MyClass
def m1; end
def m2; end;
def m3; end;
def m4; end;
public :m1
private :m2, :m3
protected :m4
end
![Page 17: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/17.jpg)
Access modifiers• public • private
– only accessible within the scope of a single object in which it is defined (truly private)
• protected– accessible from the scope of a method belonging to any object that’s
an instance of the same class
class MyClass
protected
def my_protected_method
end
public
def my_public_method(other)
self.my_protected_method
other.my_protected_method
end
end
mc1 = MyClass.new
mc2 = MyClass.new
mc1.my_public_method(mc2) #=> Works mc1.my_protected_method #=> NoMethodError
![Page 18: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/18.jpg)
Access modifiers• Quiz
class Speaker
def talk
self.confident? ? “lecture…” : “abscond!”
end
private
def confident?
true
end
end
Speaker.new.talk
What is the output? NoMethodError
Lesson:Private methods can be called only with implicit receiver. No explicit receiver. Not even self.
![Page 19: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/19.jpg)
Access modifiers• Quiz
We talked about access modifiers with methods as example. What about the access modifiers for instance variables?
Answer:
Access modifiers don’t apply to Instance variables (@inst_vari).
Instance variables always remain private.
Access to instance variables are only through getter and setter methods!
![Page 20: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/20.jpg)
Singleton MethodsA method defined to be specific to a single
object
statement = “Karthik is speaking at Ruby Conf India 2011”
def statement.really?
true
end
statement.really?
What is the output? #=> true
another_statement = “Karthik is bull shitting at Ruby Conf 2011”
another_statement.really?
What is the output? #=> undefined method error
![Page 21: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/21.jpg)
Singleton MethodsHow about a class method? Isn’t that tied to a single object?
Eg:
def MyClass.my_class_method
#blah..
end
Class methods are indeed singleton methods!
![Page 22: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/22.jpg)
Methods – where do they reside?It depends!!!
my_obj1______________________
@var_1 = 1
my_obj2______________________
MyClass______________________myclass_instance_method()
Class
Class
#<Class: MyClass>______________________
myclass_class_method()
#<Class: #<Class: 0x1234567>>______________________
myclass_class_method()my_obj1_specific_inst_meth()my_obj1_specific_class_meth()
#<Class: #<Class: 0x7654321>>______________________
myclass_class_method()my_obj2_specific_inst_meth()
![Page 23: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/23.jpg)
Methods -> Function call or Message??? • No function call. It’s all messages!
• duck.quack()– As Java programmer, I see it as looking up for
“quack” as member function in a table and call it.– As Ruby programmer, I see it as passing a message
“quack” to the object “duck”.
• No Method Overloading.
![Page 24: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/24.jpg)
Module• Used for namespacing
module MyModule
MyConstant = “MyModule::Myconstant”
class MyClass
MyConstant = “MyModule::MyClass::MyConstant”
end
end
puts MyModule::MyConstant # => MyModule::MyConstant
puts MyModule::MyClass::MyConstant #=> MyModule::MyClass::MyConstant
Another example:
ActiveRecord::Base.connection.execute(sql_query)
![Page 25: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/25.jpg)
Module• Better alternate to multiple inheritence.• DP: “Favor composition over inheritence”• Code Examples:
module MyModule
def my_meth
puts “my_meth() of MyModule”
end
end
class MyClass
end
Case 1: Include MyModule instance methods as instance methods of myClass
class MyClass
include MyModule # Just include it…
end
![Page 26: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/26.jpg)
Modulemodule MyModule
def my_meth
puts “my_meth() of MyModule”
end
end
class MyClass
end
Case 2: Include instance methods of MyModule as class methods of MyClass
class MyClass class << self include MyModule endend
class MyClass extend MyModuleend
![Page 27: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/27.jpg)
ModuleQuiz:
module MyModuledef self.my_freakin_meth
puts “my_freakin_meth() of MyModule”end
end
class MyClassinclude MyModule
end
MyClass.my_freakin_methWhat is the output?#NoMethodError
Lesson:When an object includes a module
– Module’s instance methods are included– Module’s class methods are excluded
![Page 28: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/28.jpg)
Method LookupRegular Objects
# <Class : Object>Object
# <Class : S>S
D # <Class : D>
# <Class : # <D:0x1234567>>
# <Class : # <D:0x7654321>>
# <D: 0x1234567>
# <D: 7654321>
Classes Eigen/Singleton/Ghost Classes
![Page 29: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/29.jpg)
Resources
• Programming Ruby by Dave Thomas• Metaprogramming Ruby by Paolo Perrotta• The Well-grounded Rubyist by David A Black• OneStepBack.org by Jim Weirich• blog.jayfields.com
![Page 30: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/30.jpg)
???
![Page 31: Deciphering the Ruby Object Model](https://reader034.vdocuments.mx/reader034/viewer/2022042518/5556853bd8b42abc5a8b5529/html5/thumbnails/31.jpg)
Thanks.Don’t forget to whisper your feedback in my ears!