SlideShare a Scribd company logo
Beauty of
Metaprogramming
in Ruby

S. M. Ashif Manjur
@Nascenia
• What is Metaprogramming?
• Ruby Object Model
• Sharing functionalities
• instance_eval and class_eval
• Magic of method_missing



Topics I will touch…
“Writing code that writes code”




WHAT IS METAPROGRAMMING?
METHOD LOOKUP CHAIN
• Classes are always open
• Class definitions are live
• Classes are objects
• Every method call has a
  receiver
• Anonymous or ghost classes


CLASSES
CONSOLE OUTPUT: $$$ CAR




OPEN CLASSES
“with great power comes great
           responsibility”


AS UNCLE BEN SAYS…
CONSOLE OUTPUT: $$$ CAR




MONKEY PATCHING
DEFINITIONS ARE LIVE
CLASSES ARE OBJECTS
SELF AS A RECEIVER
SINGLETON METHODS
GHOST CLASSES
CLASS METHODS ARE SAME
• Prototype
• Inheritance
• Mixin




SHARING FUNCTIONALITY
PROTOTYPE
INHERITANCE
MIXIN
AFTER MIX(ED) IN…
EVALS ARE EVIL
instance_eval
class_eval
METHOD MISSING
Q&A
THANK YOU

More Related Content

Viewers also liked

Metaprogramming in C++ - from 70's to C++17
Metaprogramming in C++ - from 70's to C++17Metaprogramming in C++ - from 70's to C++17
Metaprogramming in C++ - from 70's to C++17
Sławomir Zborowski
 
Metaprogramming With Ruby
Metaprogramming With RubyMetaprogramming With Ruby
Metaprogramming With Ruby
Farooq Ali
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
Santiago Pastorino
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
Alex Koppel
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
Jiang Yan-Ting
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
Ross Lawley
 
Metaprogramming Go
Metaprogramming GoMetaprogramming Go
Metaprogramming Go
Weng Wei
 
The Black Magic of Ruby Metaprogramming
The Black Magic of Ruby MetaprogrammingThe Black Magic of Ruby Metaprogramming
The Black Magic of Ruby Metaprogramming
itnig
 
The Art of Metaprogramming in Java
The Art of Metaprogramming in Java  The Art of Metaprogramming in Java
The Art of Metaprogramming in Java
Abdelmonaim Remani
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
scidept
 
The Eschatology of Java
The Eschatology of JavaThe Eschatology of Java
The Eschatology of Java
Abdelmonaim Remani
 
Ruby Rails Overview
Ruby Rails OverviewRuby Rails Overview
Ruby Rails Overview
Netguru
 
Python Metaprogramming
Python MetaprogrammingPython Metaprogramming
Python Metaprogramming
SDU CYBERLAB
 

Viewers also liked (13)

Metaprogramming in C++ - from 70's to C++17
Metaprogramming in C++ - from 70's to C++17Metaprogramming in C++ - from 70's to C++17
Metaprogramming in C++ - from 70's to C++17
 
Metaprogramming With Ruby
Metaprogramming With RubyMetaprogramming With Ruby
Metaprogramming With Ruby
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
 
Metaprogramming Go
Metaprogramming GoMetaprogramming Go
Metaprogramming Go
 
The Black Magic of Ruby Metaprogramming
The Black Magic of Ruby MetaprogrammingThe Black Magic of Ruby Metaprogramming
The Black Magic of Ruby Metaprogramming
 
The Art of Metaprogramming in Java
The Art of Metaprogramming in Java  The Art of Metaprogramming in Java
The Art of Metaprogramming in Java
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
The Eschatology of Java
The Eschatology of JavaThe Eschatology of Java
The Eschatology of Java
 
Ruby Rails Overview
Ruby Rails OverviewRuby Rails Overview
Ruby Rails Overview
 
Python Metaprogramming
Python MetaprogrammingPython Metaprogramming
Python Metaprogramming
 

Beauty of metaprogramming