SlideShare a Scribd company logo
1 of 9
Download to read offline
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

More from Avilay Parekh (10)

Backprop
BackpropBackprop
Backprop
 
Ai &amp; ml
Ai &amp; mlAi &amp; ml
Ai &amp; ml
 
Pupymeetup
PupymeetupPupymeetup
Pupymeetup
 
Day4
Day4Day4
Day4
 
Day3
Day3Day3
Day3
 
Day2
Day2Day2
Day2
 
Day1
Day1Day1
Day1
 
Git primer
Git primerGit primer
Git primer
 
Git undo
Git undoGit undo
Git undo
 
What is cloud computing
What is cloud computingWhat is cloud computing
What is cloud computing
 

Recently uploaded

IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServicePicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServiceRenan Moreira de Oliveira
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...Aggregage
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf
20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf
20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdfJamie (Taka) Wang
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.francesco barbera
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1DianaGray10
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 

Recently uploaded (20)

IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServicePicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf
20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf
20200723_insight_release_plan_v6.pdf20200723_insight_release_plan_v6.pdf
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 

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