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

Weitere ähnliche Inhalte

Andere mochten auch

Andere mochten auch (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