SlideShare ist ein Scribd-Unternehmen logo
1 von 24
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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3
 
Array
ArrayArray
Array
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Java
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 

Ähnlich wie Ruby object model - Understanding of object play role for ruby

Ähnlich wie Ruby object model - Understanding of object play role for ruby (20)

Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Only oop
Only oopOnly oop
Only oop
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Introduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APIIntroduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection API
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
6 Object Oriented Programming
6 Object Oriented Programming6 Object Oriented Programming
6 Object Oriented Programming
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Deciphering the Ruby Object Model
Deciphering the Ruby Object ModelDeciphering the Ruby Object Model
Deciphering the Ruby Object Model
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
 
Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Python advance
Python advancePython advance
Python advance
 
Java
JavaJava
Java
 
Reflection
ReflectionReflection
Reflection
 
Ruby Interview Questions
Ruby Interview QuestionsRuby Interview Questions
Ruby Interview Questions
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

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