SlideShare a Scribd company logo
Ruby Object Graphs
Avilay Parekh
Conventions
object Derived
Base
object, Base, and Derived are all Ruby objects.
object’s class is Derived.
Derived is a subclass of Base.
Calling Scope
c1
@flavor = ‘choc’
@calories = 100
Cookie
flavor=()
flavor ()
Class
- new()
Snack
calories=()
calories()
Module
Object
BasicObject
class Snack
def initialize
@calories = 100
end
end
class Cookie < Snack
def initialize
@flavor = „choc‟
end
end
c1 = Cookie.new
c1.calories looks in this
hierarchy.
Cookie.new looks in
this hierarchy.
For obj.method Ruby will look
in the hierarchy pointed to by
obj’s class.
For method Ruby will look in the
hierarchy pointed to by self’s
class
For @attribute Ruby will look
for the attribute in self.
Singletons
c1 Class
Define methods on a specific object that will be
available to only that object and no other object.
Not the same as the singleton creation design pattern!
This is done by “inserting” an anonymous class at the
end of the object’s class hierarchy.
Note, the picture below does not show the usual
Module, Object, BasicObject, etc.
class Cookie
# same as before
end
c1 = Cookie.new
def c1.eat
puts “chomp..chomp”
end
c2 = Cookie.new
c1.eat # => “chomp..chomp”
c2.eat # => no method found
Anon
-eat()
Cookie
Snack
Class Methods
c1
Class
Known as “static methods” in C#.
Simply a special case of singletons described in the
previous slide.
An anonymous class is inserted at the end of Cookie’s
class hierarchy.
Anon
-sweet?()
Cookie
Snack
class Cookie
# same as before
def Cookie.sweet?
true
end
end
puts Cookie.sweet?
class Cookie
# same as before
end
c1 = Cookie.new
def c1.eat
puts “chomp..chomp”
end
class << c1
def munch
puts “yummy!”
end
end
c1.eat
c1.munch
Another way to define singletons
Singletons
class Cookie
# same as before
def Cookie.sweet?
true
end
class << self
def savory?
false
end
end
end
puts Cookie.sweet?
puts Cookie.savory?
extend object with Module
c1 Class
Instance methods of the module are available as
instance methods of that specific object.
Like singleton methods, except loosely coupled.
Anon class points to the Module’s method table
instead of having the methods itself.
Done by “including” the module in a newly created
singleton class.
Anon
Cookie
Snack
module Recipe
def slice
::
end
end
class Cookie < Snack
# same as before
End
c1 = Cookie.new
c1.extend Recipe
c1.slice
Recipe
-slice
methods
extend class with Module
c1
Instance methods of the module are available as
class methods of the class.
Like class methods, except loosely coupled.
Cookie
Snack
module Recipe
def slice
::
end
end
class Cookie
extend Recipe
end
Cookie.slice
Anon
Recipe
-slice
methods
Class
include Module in class
c1 Class
Instance methods of the module are available as
instance methods of the class.
Like OO inheritance, except loosely coupled.
Done by “inserting” an anonymous class in the middle
of the object’s class hierarchy.
Cookie
Snack
Anon
Recipe
-slice
methods
module Recipe
def slice
::
end
end
class Cookie < Snack
include Recipe
# same as before
End
c1 = Cookie.new
c1.slice
c1.flavor

More Related Content

PPTX
Design pattern-presentation
PPTX
Lightning talk
PDF
Ruby object model at the Ruby drink-up of Sophia, January 2013
PDF
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
PDF
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
PDF
A07
PPT
Chapter 12
PPTX
Introduction to Ruby’s Reflection API
Design pattern-presentation
Lightning talk
Ruby object model at the Ruby drink-up of Sophia, January 2013
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
A07
Chapter 12
Introduction to Ruby’s Reflection API

More from Avilay Parekh (10)

PPTX
Backprop
PDF
Ai &amp; ml
PPTX
Pupymeetup
PPTX
PPTX
PPTX
PPTX
PPTX
Git primer
PPTX
Git undo
PDF
What is cloud computing
Backprop
Ai &amp; ml
Pupymeetup
Git primer
Git undo
What is cloud computing
Ad

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPT
Teaching material agriculture food technology
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Electronic commerce courselecture one. Pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Encapsulation theory and applications.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Machine Learning_overview_presentation.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Unlocking AI with Model Context Protocol (MCP)
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Assigned Numbers - 2025 - Bluetooth® Document
Teaching material agriculture food technology
NewMind AI Weekly Chronicles - August'25-Week II
Advanced methodologies resolving dimensionality complications for autism neur...
Electronic commerce courselecture one. Pdf
sap open course for s4hana steps from ECC to s4
Encapsulation theory and applications.pdf
Network Security Unit 5.pdf for BCA BBA.
Reach Out and Touch Someone: Haptics and Empathic Computing
Dropbox Q2 2025 Financial Results & Investor Presentation
The Rise and Fall of 3GPP – Time for a Sabbatical?
Per capita expenditure prediction using model stacking based on satellite ima...
Machine Learning_overview_presentation.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Ad

Ruby object graph

  • 2. Conventions object Derived Base object, Base, and Derived are all Ruby objects. object’s class is Derived. Derived is a subclass of Base.
  • 3. Calling Scope c1 @flavor = ‘choc’ @calories = 100 Cookie flavor=() flavor () Class - new() Snack calories=() calories() Module Object BasicObject class Snack def initialize @calories = 100 end end class Cookie < Snack def initialize @flavor = „choc‟ end end c1 = Cookie.new c1.calories looks in this hierarchy. Cookie.new looks in this hierarchy. For obj.method Ruby will look in the hierarchy pointed to by obj’s class. For method Ruby will look in the hierarchy pointed to by self’s class For @attribute Ruby will look for the attribute in self.
  • 4. Singletons c1 Class Define methods on a specific object that will be available to only that object and no other object. Not the same as the singleton creation design pattern! This is done by “inserting” an anonymous class at the end of the object’s class hierarchy. Note, the picture below does not show the usual Module, Object, BasicObject, etc. class Cookie # same as before end c1 = Cookie.new def c1.eat puts “chomp..chomp” end c2 = Cookie.new c1.eat # => “chomp..chomp” c2.eat # => no method found Anon -eat() Cookie Snack
  • 5. Class Methods c1 Class Known as “static methods” in C#. Simply a special case of singletons described in the previous slide. An anonymous class is inserted at the end of Cookie’s class hierarchy. Anon -sweet?() Cookie Snack class Cookie # same as before def Cookie.sweet? true end end puts Cookie.sweet?
  • 6. class Cookie # same as before end c1 = Cookie.new def c1.eat puts “chomp..chomp” end class << c1 def munch puts “yummy!” end end c1.eat c1.munch Another way to define singletons Singletons class Cookie # same as before def Cookie.sweet? true end class << self def savory? false end end end puts Cookie.sweet? puts Cookie.savory?
  • 7. extend object with Module c1 Class Instance methods of the module are available as instance methods of that specific object. Like singleton methods, except loosely coupled. Anon class points to the Module’s method table instead of having the methods itself. Done by “including” the module in a newly created singleton class. Anon Cookie Snack module Recipe def slice :: end end class Cookie < Snack # same as before End c1 = Cookie.new c1.extend Recipe c1.slice Recipe -slice methods
  • 8. extend class with Module c1 Instance methods of the module are available as class methods of the class. Like class methods, except loosely coupled. Cookie Snack module Recipe def slice :: end end class Cookie extend Recipe end Cookie.slice Anon Recipe -slice methods Class
  • 9. include Module in class c1 Class Instance methods of the module are available as instance methods of the class. Like OO inheritance, except loosely coupled. Done by “inserting” an anonymous class in the middle of the object’s class hierarchy. Cookie Snack Anon Recipe -slice methods module Recipe def slice :: end end class Cookie < Snack include Recipe # same as before End c1 = Cookie.new c1.slice c1.flavor