SlideShare a Scribd company logo
Ruby Object Model
This is a game of object for Ruby
Ruby is a game but Syntax is not game.
This Theory helps to solve two biggest
Questions
Which Class does any method come from ?
What happen when we include any specific module?
Ruby Objective
Key points :
Classes are Object.
Closed to modifications and Open to extend.
The Axiom
What is Kernel???
Kernel is a module which is included in the Object. Where These Method can be Call Without the
receiver and functional Form.
Eg. Array(1..5)
Hash([1,2,3])
Classes
● Approach in Ruby is different having all the classes are open.
● Overview
Problem: Make a method for all the type of string which convert it to
alphanumeric string includes special character etc ?
How To Do that ???????
Does Open Class will Help ??????????
If Yes then How ???????
Solution : Yes We can do it via Open Class of Ruby
class String
def to_alphanumeric
gsub /[^ws]/, ''
end
end
“Sys@@#tango”.to_alphanumeric => “Systango”
Open Class Problem ->
Revised method problem
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
Points to Remember:
● Class inherit from the Type Class only not by the Type Object.
● Any Creating a New Class then it goes to the object because Class is a Object.
● Objects that share the same class also share the same methods, so the
methods must be stored in the class, not the object.
● An object’s instance variables live in the object itself,and an object’s methods live in the object’s
class. That’s why objects of the same class share methods but don’t share instance variables
String.instance_methods == "abc".methods # => true
String.methods == "abc".methods # => false
What is Self ??
● 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
The joy of “self” discovery
p self
class MyClass
p self
def self.my_class_method
P self
end
def my_instance_method
P self
end
end
=> main (Which the the top level of hierarchy Object
Class)
=> MyClass (which is the type class)
=> MyClass (which is the type class explicit Reciever
is Class Name)
=> Instance of class (Object which is called this
method)
what is “main” in ruby?
puts self
class Foo
puts self
end
OUTPUT:
main
Foo
- Everything in Ruby occurs in the context of some object. The object at the top level is called "main". It's
basically an instance of Object with the special property that any methods defined there are added as
instance methods of Object (so they're available everywhere).
- Actually there is a main, but it is not a method; it's the top-level object that is the initial execution context
of a Ruby program.
Access modifiers
● Public
● Private : only accessible within the scope of a single object in which it is defined (truly private)
( Can’t call with an explicit receiver )
● Protected: accessible from the scope of a method belonging to any object that’s an instance of the
same class
(Invoked by Object of the defining class and it’s subclasses)
class MyClass
protected
def my_protected_method
end
public
def my_public_method(other)
self.my_protected_method
other.my_protected_method
end
end
#=> Works
#=> NoMethodError
mc1 = MyClass.new
mc2 = MyClass.new
mc1.my_public_method(mc2)
mc1.my_protected_method
Quiz Time
class MyClass
protected
def my_protected_method
end
def
self.my_protected_method
puts "Welcome"
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)
MyClass.my_protected_method
=> Works
=> Works
Ruby Variable Scoping
● Class Variable (@@) - Available from the Class Definition and any Subclasses
● Instance Variable (@) - Specific Object, Across all the methods in a class Instance
● Global Variable($)
● Local Variable
=> Every Object has it’s own list of Instance Variable,Independent of other Objects- even other
objects of the Same Class.
Quiz Time
class A
y=1
@p = 2
@q
@@r = 2
def initialize
@@t=3
@s = 2
end
def welcome
@indore = 1
@bhopal = 2
@jabalpur = 3
@@dhar = 3
end
end
p A.instance_variables
p A.class_variables
a = A.new
b = A.new
p a.instance_variables
p A.class_variables
p b.welcome
p b.instance_variables
=> [:@p]
=> [:@@r]
=> [:@s]
=> [:@@r, :@@t]
=> [:@s, :@indore, :@bhopal, :@jabalpur]
Module
Module is just bunch of instance method with a couple of additional features ( A superClass and
New Method)
MyModule
->MyClass
-> MyConstant
->MyConstant
There are not same
Alternative to Multiple Inheritance
Code Examples:
module MyModule
def my_meth
puts “my_meth() of MyModule”
end
end
Case 1: Include MyModule instance methods as instance methods of myClass
class MyClass
include MyModule # Just include it…
end
Case 2: Include instance methods of MyModule as class methods of MyClass
Quiz Time
module MyModule
def self.my_freakin_meth
puts “my_freakin_meth() of MyModule”
end
end
class MyClass
include MyModule
end
MyClass.my_freakin_meth
What is the output?
#NoMethodError
Lesson:
When an object includes a module
Module’s instance methods are included
Module’s class methods are excluded
Golden Points
● Objects that share the same class also share the same methods, So the Method must
be stored in the Class, not the object.
● Object’s instance variable live in the Object itself and Object’s method live in the
Object’s Class.
● Methods of an object are also the instance method of it’s Class
Eg. String.instance_methods == “abc”.methods = > True
String.methods == “abc”.methods => False due to inclusion of private and protected
method.
Any
Questions??????
Next Topics to cover:
● Design Pattern
● Practical Theory of Ruby Object
Model
● Best Practices of Rails

More Related Content

PDF
The Ruby Object Model by Rafael Magana
DOCX
Keyword of java
PPTX
Ruby object model
PPTX
Advanced Python : Static and Class Methods
PPT
The ruby programming language
PDF
Ruby — An introduction
PDF
Python Class | Python Programming | Python Tutorial | Edureka
PDF
Java keywords
The Ruby Object Model by Rafael Magana
Keyword of java
Ruby object model
Advanced Python : Static and Class Methods
The ruby programming language
Ruby — An introduction
Python Class | Python Programming | Python Tutorial | Edureka
Java keywords

What's hot (20)

PPTX
Static keyword ppt
PPTX
6. static keyword
PDF
CLASS & OBJECT IN JAVA
PPTX
Object Oriented Programming_Lecture 2
PPTX
‫Object Oriented Programming_Lecture 3
PDF
PDF
Lect 1-java object-classes
PDF
Chapter 01 Introduction to Java by Tushar B Kute
PDF
Object Oriented Programming in PHP
PPTX
Java static keyword
PPTX
Java class,object,method introduction
PPTX
Week9 Intro to classes and objects in Java
PPT
Core Java Programming | Data Type | operator | java Control Flow| Class 2
PDF
Keywords and classes
PPTX
Inheritance in Java
PDF
Metaprogramming in Ruby
PPT
Java static keyword
PPT
Classes&amp;objects
PDF
Python - object oriented
PPTX
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Static keyword ppt
6. static keyword
CLASS & OBJECT IN JAVA
Object Oriented Programming_Lecture 2
‫Object Oriented Programming_Lecture 3
Lect 1-java object-classes
Chapter 01 Introduction to Java by Tushar B Kute
Object Oriented Programming in PHP
Java static keyword
Java class,object,method introduction
Week9 Intro to classes and objects in Java
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Keywords and classes
Inheritance in Java
Metaprogramming in Ruby
Java static keyword
Classes&amp;objects
Python - object oriented
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Ad

Similar to Ruby object model - Understanding of object play role for ruby (20)

KEY
Metaprogramming Primer (Part 1)
PPTX
class as the basis.pptx
PPTX
Only oop
PPTX
Object oriented programming in java
PPTX
Introduction to Ruby’s Reflection API
PPTX
Object Oriented Programming.pptx
PPTX
Object oriented javascript
PPT
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
PPTX
6 Object Oriented Programming
PPTX
classes-objects in oops java-201023154255.pptx
PPT
Introduction to Python - Part Three
PPTX
Deciphering the Ruby Object Model
PPTX
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
PPTX
Java As an OOP Language,Exception Handling & Applets
PDF
Ruby object model at the Ruby drink-up of Sophia, January 2013
PPTX
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
PPTX
Classes objects in java
PPTX
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
PPTX
Abstraction in java [abstract classes and Interfaces
PPTX
Python 2. classes- cruciql for students objects1.pptx
Metaprogramming Primer (Part 1)
class as the basis.pptx
Only oop
Object oriented programming in java
Introduction to Ruby’s Reflection API
Object Oriented Programming.pptx
Object oriented javascript
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
6 Object Oriented Programming
classes-objects in oops java-201023154255.pptx
Introduction to Python - Part Three
Deciphering the Ruby Object Model
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Java As an OOP Language,Exception Handling & Applets
Ruby object model at the Ruby drink-up of Sophia, January 2013
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
Classes objects in java
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
Abstraction in java [abstract classes and Interfaces
Python 2. classes- cruciql for students objects1.pptx
Ad

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Modernizing your data center with Dell and AMD
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
KodekX | Application Modernization Development
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Empathic Computing: Creating Shared Understanding
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Modernizing your data center with Dell and AMD
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Network Security Unit 5.pdf for BCA BBA.
Advanced methodologies resolving dimensionality complications for autism neur...
Digital-Transformation-Roadmap-for-Companies.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
KodekX | Application Modernization Development
Unlocking AI with Model Context Protocol (MCP)
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Encapsulation_ Review paper, used for researhc scholars
Empathic Computing: Creating Shared Understanding
Spectral efficient network and resource selection model in 5G networks
20250228 LYD VKU AI Blended-Learning.pptx

Ruby object model - Understanding of object play role for ruby

  • 1. Ruby Object Model This is a game of object for Ruby
  • 2. Ruby is a game but Syntax is not game.
  • 3. This Theory helps to solve two biggest Questions Which Class does any method come from ? What happen when we include any specific module?
  • 4. Ruby Objective Key points : Classes are Object. Closed to modifications and Open to extend.
  • 6. What is Kernel??? Kernel is a module which is included in the Object. Where These Method can be Call Without the receiver and functional Form. Eg. Array(1..5) Hash([1,2,3])
  • 7. Classes ● Approach in Ruby is different having all the classes are open. ● Overview Problem: Make a method for all the type of string which convert it to alphanumeric string includes special character etc ? How To Do that ??????? Does Open Class will Help ?????????? If Yes then How ???????
  • 8. Solution : Yes We can do it via Open Class of Ruby class String def to_alphanumeric gsub /[^ws]/, '' end end “Sys@@#tango”.to_alphanumeric => “Systango” Open Class Problem -> Revised method problem
  • 9. 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
  • 10. Points to Remember: ● Class inherit from the Type Class only not by the Type Object. ● Any Creating a New Class then it goes to the object because Class is a Object. ● Objects that share the same class also share the same methods, so the methods must be stored in the class, not the object. ● An object’s instance variables live in the object itself,and an object’s methods live in the object’s class. That’s why objects of the same class share methods but don’t share instance variables String.instance_methods == "abc".methods # => true String.methods == "abc".methods # => false
  • 11. What is Self ?? ● 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
  • 12. The joy of “self” discovery p self class MyClass p self def self.my_class_method P self end def my_instance_method P self end end => main (Which the the top level of hierarchy Object Class) => MyClass (which is the type class) => MyClass (which is the type class explicit Reciever is Class Name) => Instance of class (Object which is called this method)
  • 13. what is “main” in ruby? puts self class Foo puts self end OUTPUT: main Foo - Everything in Ruby occurs in the context of some object. The object at the top level is called "main". It's basically an instance of Object with the special property that any methods defined there are added as instance methods of Object (so they're available everywhere). - Actually there is a main, but it is not a method; it's the top-level object that is the initial execution context of a Ruby program.
  • 14. Access modifiers ● Public ● Private : only accessible within the scope of a single object in which it is defined (truly private) ( Can’t call with an explicit receiver ) ● Protected: accessible from the scope of a method belonging to any object that’s an instance of the same class (Invoked by Object of the defining class and it’s subclasses) class MyClass protected def my_protected_method end public def my_public_method(other) self.my_protected_method other.my_protected_method end end #=> Works #=> NoMethodError mc1 = MyClass.new mc2 = MyClass.new mc1.my_public_method(mc2) mc1.my_protected_method
  • 15. Quiz Time class MyClass protected def my_protected_method end def self.my_protected_method puts "Welcome" 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) MyClass.my_protected_method => Works => Works
  • 16. Ruby Variable Scoping ● Class Variable (@@) - Available from the Class Definition and any Subclasses ● Instance Variable (@) - Specific Object, Across all the methods in a class Instance ● Global Variable($) ● Local Variable => Every Object has it’s own list of Instance Variable,Independent of other Objects- even other objects of the Same Class.
  • 17. Quiz Time class A y=1 @p = 2 @q @@r = 2 def initialize @@t=3 @s = 2 end def welcome @indore = 1 @bhopal = 2 @jabalpur = 3 @@dhar = 3 end end p A.instance_variables p A.class_variables a = A.new b = A.new p a.instance_variables p A.class_variables p b.welcome p b.instance_variables => [:@p] => [:@@r] => [:@s] => [:@@r, :@@t] => [:@s, :@indore, :@bhopal, :@jabalpur]
  • 18. Module Module is just bunch of instance method with a couple of additional features ( A superClass and New Method) MyModule ->MyClass -> MyConstant ->MyConstant There are not same Alternative to Multiple Inheritance Code Examples: module MyModule def my_meth puts “my_meth() of MyModule” end end
  • 19. Case 1: Include MyModule instance methods as instance methods of myClass class MyClass include MyModule # Just include it… end
  • 20. Case 2: Include instance methods of MyModule as class methods of MyClass
  • 21. Quiz Time module MyModule def self.my_freakin_meth puts “my_freakin_meth() of MyModule” end end class MyClass include MyModule end MyClass.my_freakin_meth What is the output? #NoMethodError Lesson: When an object includes a module Module’s instance methods are included Module’s class methods are excluded
  • 22. Golden Points ● Objects that share the same class also share the same methods, So the Method must be stored in the Class, not the object. ● Object’s instance variable live in the Object itself and Object’s method live in the Object’s Class. ● Methods of an object are also the instance method of it’s Class Eg. String.instance_methods == “abc”.methods = > True String.methods == “abc”.methods => False due to inclusion of private and protected method.
  • 24. Next Topics to cover: ● Design Pattern ● Practical Theory of Ruby Object Model ● Best Practices of Rails