SlideShare a Scribd company logo
Deciphering the Ruby Object Model
Who am I?KarthikSirasanagandlaA Pragmatic Programmer @ ThoughtWorksVB freelance programmer for about a yearJava/JEE apps. developer for about 5 yearsRuby Enthusiast for over 8 months nowHaskell newbieOccasionally blog at kartzontech.blogspot.comNot so frequently tweet using @kartzontech
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
The AxiomBasicObjectKernelSuperclassObjectClassSuperclassSuperclassModuleAnyOtherClassSuperclassYou don’t question an axiom,rather start with it…ClassClass
ClassesClasses should be closed for modification and open to extensionclass MyClassendclass MyClassdef im# instance method	puts “Instance method” end	def self.cm	      #class method	puts “Class method”endend
Classes“With more power comes more responsibilities” opines Matz,  like uncle Ben in the spiderman movie Open Classes ???Monkey Patching ???
ClassesClass methods can be defined in number of ways:class MyClassdef MyClass.cm1	puts “cm1() invoked…” end	def self.cm2	puts “cm2() invoked…”end	class << self		def cm3		        puts “cm3() invoked…”		endendend
ClassesClasses are objectsclass MyClass def im	puts “Instance method” end endis the same as:MyClass = Class.newdo	def im	puts “Class method”endend
ClassesQuiz Time:class MyClass < String.new def self.to_s	puts “Class method” end endMyClass.to_sWhat is the output? TypeError: Wrong argument type String (expected Class)Lesson:Classes inherit from classes only not from just any object
ClassesNo connection between a class and its objects’ instance variablesMyClass______________________my_instance_method()ClassClassClassmy_obj1______________________@var_1 = 1@var_2 = “Ruby Conf”my_obj2______________________@v = 123my_obj3______________________
ClassesQuiz: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.
The joy of “self” discoveryself is not the same as this in Javaself is synonymous to current/default objectWho gets to be self    depends on     Where self is
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}”	endend
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 endputs “Class instance variables of A are #{A.instance_variables}”[:@p]puts “Class variables of A are #{A.class_variables}”[:@@t]a = A.newputs “a, of type A, has instance variables #{a.instance_variables}”[:@s]puts “Class variables of A are #{A.class_variables}”[:@@t, :@@r]
Access modifiersRuby has 3 access modifiers for methods:Public (default)PrivateProtectedAccess modifiers apply until the end of scope, or until another access modifier pops-upclass MyClass	#public is the default access modifier	def m1; endprivate	def m2; end;	def m3; end;protected	def m4; end;end
Access modifiersprivate, public, protectedare all methods, so you can pass method name as symbol to it and change its visibilityclass MyClass	def m1; end	def m2; end;	def m3; end;	def m4; end;public :m1private :m2, :m3protected :m4end
Access modifierspublic privateonly accessible within the scope of a single object in which it is defined (truly private)protectedaccessible from the scope of a method belonging to any object that’s an instance of the same classclass MyClassprotected	def my_protected_method	endpublic	def my_public_method(other)self.my_protected_methodother.my_protected_method	endendmc1 = MyClass.newmc2 = MyClass.newmc1.my_public_method(mc2)		#=> Works mc1.my_protected_method		#=> NoMethodError
Access modifiersQuizclass Speaker	def  talkself.confident? ? “lecture…” : “abscond!”	endprivate	def  confident?	     true	endendSpeaker.new.talkWhat is the output? NoMethodErrorLesson:Private methods can be called only with implicit receiver. No explicit receiver. Not even self.
Access modifiersQuizWe 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!
Singleton MethodsA method defined to be specific to a single objectstatement = “Karthik is speaking at Ruby Conf India 2011” def statement.really?	trueendstatement.really?  	What is the output?	 #=> trueanother_statement = “Karthik is bull shitting at Ruby Conf 2011”another_statement.really?	What is the output? 	#=> undefined method error
Singleton MethodsHow about a class method? Isn’t that tied to a single object?Eg:def MyClass.my_class_method#blah..endClass methods are indeed singleton methods!
Methods – where do they reside?It depends!!!MyClass______________________myclass_instance_method()#<Class: MyClass>______________________myclass_class_method()Class#<Class: #<Class: 0x1234567>>______________________myclass_class_method()my_obj1_specific_inst_meth()my_obj1_specific_class_meth()my_obj1______________________@var_1 = 1Class#<Class: #<Class: 0x7654321>>______________________myclass_class_method()my_obj2_specific_inst_meth()my_obj2______________________
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.
Module Used for namespacingmodule MyModuleMyConstant = “MyModule::Myconstant”	class MyClassMyConstant = “MyModule::MyClass::MyConstant”	endendputs MyModule::MyConstant# => MyModule::MyConstantputs MyModule::MyClass::MyConstant#=> MyModule::MyClass::MyConstantAnother example:ActiveRecord::Base.connection.execute(sql_query)
ModuleBetter alternate to multiple inheritence.DP: “Favor composition over inheritence”Code Examples:module MyModule	def my_meth		puts “my_meth() of MyModule”	endend	class MyClassendCase 1: Include MyModule instance methods as instance methods of myClassclass MyClass	include MyModule    #   Just include it…end
Modulemodule MyModule	def my_meth		puts “my_meth() of MyModule”	endend	class MyClassendCase 2: Include instance methods of MyModule as class methods of MyClass
ModuleQuiz:module MyModule	def self.my_freakin_meth		puts “my_freakin_meth() of MyModule”	endend	class MyClass	include MyModuleendMyClass.my_freakin_methWhat is the output?#NoMethodErrorLesson:When an object includes a moduleModule’s instance methods are includedModule’s class methods are excluded
Method LookupEigen/Singleton/Ghost ClassesRegular ObjectsClasses# <Class : Object>Object# <Class : S>SD# <Class : D># <D: 0x1234567># <Class : # <D:0x1234567>># <Class : # <D:0x7654321>># <D: 7654321>
ResourcesProgramming Ruby by Dave ThomasMetaprogramming Ruby by Paolo PerrottaThe Well-grounded Rubyist by David A BlackOneStepBack.org by Jim Weirichblog.jayfields.com
???
Thanks.Don’t forget to whisper your feedback in my ears!

More Related Content

What's hot (19)

OOP in C#
OOP in C#OOP in C#
OOP in C#
DevMix
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
Lovely Professional University
 
Super and final in java
Super and final in javaSuper and final in java
Super and final in java
anshu_atri
 
Identifier
IdentifierIdentifier
Identifier
ASHUTOSH TRIVEDI
 
Delegate - KhanhLD
Delegate - KhanhLDDelegate - KhanhLD
Delegate - KhanhLD
Framgia Vietnam
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
homeworkping9
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
tigerwarn
 
learn Ruby in AMC Square learning
learn Ruby in AMC Square learninglearn Ruby in AMC Square learning
learn Ruby in AMC Square learning
ASIT Education
 
OOP interview questions & answers.
OOP interview questions & answers.OOP interview questions & answers.
OOP interview questions & answers.
Questpond
 
Metaprogramming Rails
Metaprogramming RailsMetaprogramming Rails
Metaprogramming Rails
Justus Eapen
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to java
SadhanaParameswaran
 
Abstract method
Abstract methodAbstract method
Abstract method
Yaswanth Babu Gummadivelli
 
Valued Conversions
Valued ConversionsValued Conversions
Valued Conversions
Kevlin Henney
 
Object Oriended Programming with Java
Object Oriended Programming with JavaObject Oriended Programming with Java
Object Oriended Programming with Java
Jakir Hossain
 
A350103
A350103A350103
A350103
aijbm
 
c# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventionsc# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventions
Micheal Ogundero
 
Meta Programming in Groovy
Meta Programming in GroovyMeta Programming in Groovy
Meta Programming in Groovy
NexThoughts Technologies
 
Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby Metaprogramming
Wei Jen Lu
 
What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | Edureka
Edureka!
 
OOP in C#
OOP in C#OOP in C#
OOP in C#
DevMix
 
Super and final in java
Super and final in javaSuper and final in java
Super and final in java
anshu_atri
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
homeworkping9
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
tigerwarn
 
learn Ruby in AMC Square learning
learn Ruby in AMC Square learninglearn Ruby in AMC Square learning
learn Ruby in AMC Square learning
ASIT Education
 
OOP interview questions & answers.
OOP interview questions & answers.OOP interview questions & answers.
OOP interview questions & answers.
Questpond
 
Metaprogramming Rails
Metaprogramming RailsMetaprogramming Rails
Metaprogramming Rails
Justus Eapen
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to java
SadhanaParameswaran
 
Object Oriended Programming with Java
Object Oriended Programming with JavaObject Oriended Programming with Java
Object Oriended Programming with Java
Jakir Hossain
 
A350103
A350103A350103
A350103
aijbm
 
c# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventionsc# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventions
Micheal Ogundero
 
Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby Metaprogramming
Wei Jen Lu
 
What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | Edureka
Edureka!
 

Viewers also liked (12)

Why I hated the daily stand-up and how I started enjoying it?
Why I hated the daily stand-up and how I started enjoying it?Why I hated the daily stand-up and how I started enjoying it?
Why I hated the daily stand-up and how I started enjoying it?
Karthik Sirasanagandla
 
Agile smells
Agile smellsAgile smells
Agile smells
Karthik Sirasanagandla
 
Agile Testing Anti-Patterns and Rescue Strategies (Version2)
Agile Testing Anti-Patterns and Rescue Strategies (Version2)Agile Testing Anti-Patterns and Rescue Strategies (Version2)
Agile Testing Anti-Patterns and Rescue Strategies (Version2)
Karthik Sirasanagandla
 
Git for-fun-and-productivity
Git for-fun-and-productivityGit for-fun-and-productivity
Git for-fun-and-productivity
Karthik Sirasanagandla
 
When Agile becomes fragile
When Agile becomes fragileWhen Agile becomes fragile
When Agile becomes fragile
Karthik Sirasanagandla
 
Agile hardware
Agile hardware Agile hardware
Agile hardware
seeedstudio
 
Agile Adoption Story in LGE (Aps2010)
Agile Adoption Story in LGE (Aps2010)Agile Adoption Story in LGE (Aps2010)
Agile Adoption Story in LGE (Aps2010)
Woogon Shim
 
Integrating Hardware (Waterfall) and Software (Agile) Development
Integrating Hardware (Waterfall) and Software (Agile) DevelopmentIntegrating Hardware (Waterfall) and Software (Agile) Development
Integrating Hardware (Waterfall) and Software (Agile) Development
Intland Software GmbH
 
When agile-becomes-fragile
When agile-becomes-fragileWhen agile-becomes-fragile
When agile-becomes-fragile
Karthik Sirasanagandla
 
Introduction to Agile Hardware
Introduction to Agile Hardware Introduction to Agile Hardware
Introduction to Agile Hardware
Cprime
 
Understanding Agile Hardware
Understanding Agile HardwareUnderstanding Agile Hardware
Understanding Agile Hardware
Cprime
 
Agile Test Automation Anti-patterns and Rescue Strategies
Agile Test Automation Anti-patterns and Rescue StrategiesAgile Test Automation Anti-patterns and Rescue Strategies
Agile Test Automation Anti-patterns and Rescue Strategies
Karthik Sirasanagandla
 
Why I hated the daily stand-up and how I started enjoying it?
Why I hated the daily stand-up and how I started enjoying it?Why I hated the daily stand-up and how I started enjoying it?
Why I hated the daily stand-up and how I started enjoying it?
Karthik Sirasanagandla
 
Agile Testing Anti-Patterns and Rescue Strategies (Version2)
Agile Testing Anti-Patterns and Rescue Strategies (Version2)Agile Testing Anti-Patterns and Rescue Strategies (Version2)
Agile Testing Anti-Patterns and Rescue Strategies (Version2)
Karthik Sirasanagandla
 
Agile Adoption Story in LGE (Aps2010)
Agile Adoption Story in LGE (Aps2010)Agile Adoption Story in LGE (Aps2010)
Agile Adoption Story in LGE (Aps2010)
Woogon Shim
 
Integrating Hardware (Waterfall) and Software (Agile) Development
Integrating Hardware (Waterfall) and Software (Agile) DevelopmentIntegrating Hardware (Waterfall) and Software (Agile) Development
Integrating Hardware (Waterfall) and Software (Agile) Development
Intland Software GmbH
 
Introduction to Agile Hardware
Introduction to Agile Hardware Introduction to Agile Hardware
Introduction to Agile Hardware
Cprime
 
Understanding Agile Hardware
Understanding Agile HardwareUnderstanding Agile Hardware
Understanding Agile Hardware
Cprime
 
Agile Test Automation Anti-patterns and Rescue Strategies
Agile Test Automation Anti-patterns and Rescue StrategiesAgile Test Automation Anti-patterns and Rescue Strategies
Agile Test Automation Anti-patterns and Rescue Strategies
Karthik Sirasanagandla
 
Ad

Similar to Deciphering the Ruby Object Model (20)

Unusual C# - OOP
Unusual C# - OOPUnusual C# - OOP
Unusual C# - OOP
Medhat Dawoud
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
Chamnap Chhorn
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
Ravi Bhadauria
 
Viva file
Viva fileViva file
Viva file
anupamasingh87
 
Only oop
Only oopOnly oop
Only oop
anitarooge
 
Ruby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyRuby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for ruby
Tushar Pal
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
Application package
Application packageApplication package
Application package
JAYAARC
 
Mca2030 object oriented programming – c++
Mca2030  object oriented programming – c++Mca2030  object oriented programming – c++
Mca2030 object oriented programming – c++
smumbahelp
 
Mca2030 object oriented programming – c++
Mca2030  object oriented programming – c++Mca2030  object oriented programming – c++
Mca2030 object oriented programming – c++
smumbahelp
 
Object Oriented Javascript part2
Object Oriented Javascript part2Object Oriented Javascript part2
Object Oriented Javascript part2
Usman Mehmood
 
C# interview quesions
C# interview quesionsC# interview quesions
C# interview quesions
Shashwat Shriparv
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
Omid Sohrabi
 
Java scjp-part1
Java scjp-part1Java scjp-part1
Java scjp-part1
Raghavendra V Gayakwad
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
Arun Vasanth
 
The Black Magic of Ruby Metaprogramming
The Black Magic of Ruby MetaprogrammingThe Black Magic of Ruby Metaprogramming
The Black Magic of Ruby Metaprogramming
itnig
 
python.pptx
python.pptxpython.pptx
python.pptx
Dhanushrajucm
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
Kuntal Bhowmick
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
How to not suck at JavaScript
How to not suck at JavaScriptHow to not suck at JavaScript
How to not suck at JavaScript
tmont
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
Ravi Bhadauria
 
Ruby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyRuby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for ruby
Tushar Pal
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
Application package
Application packageApplication package
Application package
JAYAARC
 
Mca2030 object oriented programming – c++
Mca2030  object oriented programming – c++Mca2030  object oriented programming – c++
Mca2030 object oriented programming – c++
smumbahelp
 
Mca2030 object oriented programming – c++
Mca2030  object oriented programming – c++Mca2030  object oriented programming – c++
Mca2030 object oriented programming – c++
smumbahelp
 
Object Oriented Javascript part2
Object Oriented Javascript part2Object Oriented Javascript part2
Object Oriented Javascript part2
Usman Mehmood
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
Omid Sohrabi
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
Arun Vasanth
 
The Black Magic of Ruby Metaprogramming
The Black Magic of Ruby MetaprogrammingThe Black Magic of Ruby Metaprogramming
The Black Magic of Ruby Metaprogramming
itnig
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
Kuntal Bhowmick
 
How to not suck at JavaScript
How to not suck at JavaScriptHow to not suck at JavaScript
How to not suck at JavaScript
tmont
 
Ad

Recently uploaded (20)

Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too LateKubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too LateKubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 

Deciphering the Ruby Object Model

  • 1. Deciphering the Ruby Object Model
  • 2. Who am I?KarthikSirasanagandlaA Pragmatic Programmer @ ThoughtWorksVB freelance programmer for about a yearJava/JEE apps. developer for about 5 yearsRuby Enthusiast for over 8 months nowHaskell newbieOccasionally blog at kartzontech.blogspot.comNot so frequently tweet using @kartzontech
  • 3. 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
  • 5. ClassesClasses should be closed for modification and open to extensionclass MyClassendclass MyClassdef im# instance method puts “Instance method” end def self.cm #class method puts “Class method”endend
  • 6. Classes“With more power comes more responsibilities” opines Matz, like uncle Ben in the spiderman movie Open Classes ???Monkey Patching ???
  • 7. ClassesClass methods can be defined in number of ways:class MyClassdef MyClass.cm1 puts “cm1() invoked…” end def self.cm2 puts “cm2() invoked…”end class << self def cm3 puts “cm3() invoked…” endendend
  • 8. ClassesClasses are objectsclass MyClass def im puts “Instance method” end endis the same as:MyClass = Class.newdo def im puts “Class method”endend
  • 9. ClassesQuiz Time:class MyClass < String.new def self.to_s puts “Class method” end endMyClass.to_sWhat is the output? TypeError: Wrong argument type String (expected Class)Lesson:Classes inherit from classes only not from just any object
  • 10. ClassesNo connection between a class and its objects’ instance variablesMyClass______________________my_instance_method()ClassClassClassmy_obj1______________________@var_1 = 1@var_2 = “Ruby Conf”my_obj2______________________@v = 123my_obj3______________________
  • 11. ClassesQuiz: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.
  • 12. The joy of “self” discoveryself is not the same as this in Javaself is synonymous to current/default objectWho gets to be self depends on Where self is
  • 13. 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}” endend
  • 14. 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 endputs “Class instance variables of A are #{A.instance_variables}”[:@p]puts “Class variables of A are #{A.class_variables}”[:@@t]a = A.newputs “a, of type A, has instance variables #{a.instance_variables}”[:@s]puts “Class variables of A are #{A.class_variables}”[:@@t, :@@r]
  • 15. Access modifiersRuby has 3 access modifiers for methods:Public (default)PrivateProtectedAccess modifiers apply until the end of scope, or until another access modifier pops-upclass MyClass #public is the default access modifier def m1; endprivate def m2; end; def m3; end;protected def m4; end;end
  • 16. Access modifiersprivate, public, protectedare all methods, so you can pass method name as symbol to it and change its visibilityclass MyClass def m1; end def m2; end; def m3; end; def m4; end;public :m1private :m2, :m3protected :m4end
  • 17. Access modifierspublic privateonly accessible within the scope of a single object in which it is defined (truly private)protectedaccessible from the scope of a method belonging to any object that’s an instance of the same classclass MyClassprotected def my_protected_method endpublic def my_public_method(other)self.my_protected_methodother.my_protected_method endendmc1 = MyClass.newmc2 = MyClass.newmc1.my_public_method(mc2) #=> Works mc1.my_protected_method #=> NoMethodError
  • 18. Access modifiersQuizclass Speaker def talkself.confident? ? “lecture…” : “abscond!” endprivate def confident? true endendSpeaker.new.talkWhat is the output? NoMethodErrorLesson:Private methods can be called only with implicit receiver. No explicit receiver. Not even self.
  • 19. Access modifiersQuizWe 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!
  • 20. Singleton MethodsA method defined to be specific to a single objectstatement = “Karthik is speaking at Ruby Conf India 2011” def statement.really? trueendstatement.really? What is the output? #=> trueanother_statement = “Karthik is bull shitting at Ruby Conf 2011”another_statement.really? What is the output? #=> undefined method error
  • 21. Singleton MethodsHow about a class method? Isn’t that tied to a single object?Eg:def MyClass.my_class_method#blah..endClass methods are indeed singleton methods!
  • 22. Methods – where do they reside?It depends!!!MyClass______________________myclass_instance_method()#<Class: MyClass>______________________myclass_class_method()Class#<Class: #<Class: 0x1234567>>______________________myclass_class_method()my_obj1_specific_inst_meth()my_obj1_specific_class_meth()my_obj1______________________@var_1 = 1Class#<Class: #<Class: 0x7654321>>______________________myclass_class_method()my_obj2_specific_inst_meth()my_obj2______________________
  • 23. 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.
  • 24. Module Used for namespacingmodule MyModuleMyConstant = “MyModule::Myconstant” class MyClassMyConstant = “MyModule::MyClass::MyConstant” endendputs MyModule::MyConstant# => MyModule::MyConstantputs MyModule::MyClass::MyConstant#=> MyModule::MyClass::MyConstantAnother example:ActiveRecord::Base.connection.execute(sql_query)
  • 25. ModuleBetter alternate to multiple inheritence.DP: “Favor composition over inheritence”Code Examples:module MyModule def my_meth puts “my_meth() of MyModule” endend class MyClassendCase 1: Include MyModule instance methods as instance methods of myClassclass MyClass include MyModule #   Just include it…end
  • 26. Modulemodule MyModule def my_meth puts “my_meth() of MyModule” endend class MyClassendCase 2: Include instance methods of MyModule as class methods of MyClass
  • 27. ModuleQuiz:module MyModule def self.my_freakin_meth puts “my_freakin_meth() of MyModule” endend class MyClass include MyModuleendMyClass.my_freakin_methWhat is the output?#NoMethodErrorLesson:When an object includes a moduleModule’s instance methods are includedModule’s class methods are excluded
  • 28. Method LookupEigen/Singleton/Ghost ClassesRegular ObjectsClasses# <Class : Object>Object# <Class : S>SD# <Class : D># <D: 0x1234567># <Class : # <D:0x1234567>># <Class : # <D:0x7654321>># <D: 7654321>
  • 29. ResourcesProgramming Ruby by Dave ThomasMetaprogramming Ruby by Paolo PerrottaThe Well-grounded Rubyist by David A BlackOneStepBack.org by Jim Weirichblog.jayfields.com
  • 30. ???
  • 31. Thanks.Don’t forget to whisper your feedback in my ears!