SlideShare a Scribd company logo
Ruby Metaprogramming
About me Lu Wei Jen http://blog.weijen.net http://www.facebook.com/weijenlu Twitter: weijenlu
Metaprogramming Metaprogramming is writing code that manipulates language constructs at runtime.
Agenda Object, Class and Module Singleton Methods and Metaclass Block Eval family
Part 1. Object, Class and Module Object introspection Classes  Module Singleton Method Method Lookup Dynamic Methods Hook methods
Object Introspection
Metaprogramming Ruby p.38 Objects of the same class share methods, but they don’t share instance variables.
Classes Classes themselves are nothing but  objects . Classes are instances of a class called  Class .
Method Lookup One step to the right, then up. obj @val MyClass my_method() class() Object
Open Class You can always reopen existing classes, and modify them.
Module Modules are also objects. When you include a module: Ruby creates an anonymous class wraps the module. Ruby then insert anonymous class into the ancestors chain.
Module obj @val Myclass my_method (M) m_method Object class
Singleton Method
Class Method are Singleton Method
Metaclass Where is singleton method? An object can have its own special, hidden class named Metaclass. Also named eigenclass. obj @val Myclass my_method (M) m_method Object class
Metaclass
Define Class Methods
What about module?
Method Lookup Review obj @val MyClass my_method (M) m_method Object class #obj my_singleton_method #MyClass class_method_1 class_method_2 #(M) m_class_method #Object
Define Methods Dynamically
Calling Method Dynamically
method_missing() If method can’t be find, then  self  calling method_missing(). It’s an instance method of  Kernel  that every object inherits. It responded by raising a  NoMethodError .
Overriding method_missing()
 
Hook methods inherited() included()
Part 2. Block Block Scope Callable Objects
Block
Scope (But don’t do that)
Scope Gates Class definitions Module definitions methods
Proc Object
The & Operator A block can be an argument to a method. Block argument: must be  the last  in the list of arguments. Must  prefixed  by an  &  sign. &  operator convert the  block to a Proc object. &  operator also can convert the  Proc object to block.
The & Operator
Part 3. Eval* family eval instance_eval module_eval(class_eval)
Kernel#eval() it takes a string of code. It executes the code in the string.
The troble with eval() Your editor’s features mayn’t support. Not easy to read and modify. Not easy to debug. Code Injection
Object#instance_eval
Module#class_eval(module_eval)
Classes themselves are nothing but  objects .
Thanks
Q&A

More Related Content

PPTX
Hemajava
PPTX
Mutable and immutable classes
PDF
Java unit 7
PPTX
Chapter 8 java
PPTX
Introduction to OOP with java
DOC
Java interview questions
PPTX
Std 12 computer chapter 8 classes and object in java (part 2)
Hemajava
Mutable and immutable classes
Java unit 7
Chapter 8 java
Introduction to OOP with java
Java interview questions
Std 12 computer chapter 8 classes and object in java (part 2)

What's hot (20)

DOCX
Java interview questions and answers
DOCX
Interview Questions and Answers for Java
PPTX
Unit 4 exceptions and threads
PDF
Java - Basic Concepts
PPTX
Basic concept of class, method , command line-argument
PPTX
Week9 Intro to classes and objects in Java
PDF
Classes and objects in java
PPTX
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
DOCX
Java interview questions
DOCX
Keyword of java
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPTX
Chapter 7 java
PPTX
Java static keyword
DOCX
Java Core Parctical
PPTX
Unit3 part1-class
PDF
Python Class | Python Programming | Python Tutorial | Edureka
PPT
Object and Classes in Java
PPT
Java lec class, objects and constructors
PPTX
Chapter 3
PPTX
Java interview questions and answers
Interview Questions and Answers for Java
Unit 4 exceptions and threads
Java - Basic Concepts
Basic concept of class, method , command line-argument
Week9 Intro to classes and objects in Java
Classes and objects in java
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Java interview questions
Keyword of java
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 7 java
Java static keyword
Java Core Parctical
Unit3 part1-class
Python Class | Python Programming | Python Tutorial | Edureka
Object and Classes in Java
Java lec class, objects and constructors
Chapter 3
Ad

Viewers also liked (18)

KEY
把鐵路開進視窗裡
KEY
BDD for Rails Legacy Code
ODP
BDD for Ruby
ODP
Mock Aren't Stub 讀後心得
PPT
Presentation1
PPT
SESC / Biennial by Ilaria Montorsi and Giovanni Saladino
PPT
Fiberstars Design Presentation 2009
PPT
PME DJ Service Presentation
PDF
Trend Report
PPT
Metaprogramming With Ruby
PDF
Metaprogramming
PDF
Ruby Metaprogramming 08
PPTX
Ruby Metaprogramming
PPTX
Metaprogramming ruby
PDF
Metaprogramming in Ruby
PDF
Ruby An Introduction
PPTX
The Black Magic of Ruby Metaprogramming
PPT
Introduction to Ruby on Rails
把鐵路開進視窗裡
BDD for Rails Legacy Code
BDD for Ruby
Mock Aren't Stub 讀後心得
Presentation1
SESC / Biennial by Ilaria Montorsi and Giovanni Saladino
Fiberstars Design Presentation 2009
PME DJ Service Presentation
Trend Report
Metaprogramming With Ruby
Metaprogramming
Ruby Metaprogramming 08
Ruby Metaprogramming
Metaprogramming ruby
Metaprogramming in Ruby
Ruby An Introduction
The Black Magic of Ruby Metaprogramming
Introduction to Ruby on Rails
Ad

Similar to Ruby Metaprogramming (20)

PDF
Metaprogramming ruby
PDF
Metaprogramming in Ruby
PPTX
Metaprogramming in Ruby
PDF
Ruby — An introduction
PPTX
Deciphering the Ruby Object Model
PDF
Ruby basics
PDF
Introduction to Ruby Programming Language
KEY
Metaprogramming Primer (Part 1)
PPTX
Ruby object model
PDF
The Ruby Object Model by Rafael Magana
PPT
name name2 n2.ppt
PPT
name name2 n
PPT
name name2 n2
PPT
name name2 n
PPT
ppt18
PPT
ppt9
PPT
ppt7
PPT
ppt30
PPT
ppt21
PPT
ppt2
Metaprogramming ruby
Metaprogramming in Ruby
Metaprogramming in Ruby
Ruby — An introduction
Deciphering the Ruby Object Model
Ruby basics
Introduction to Ruby Programming Language
Metaprogramming Primer (Part 1)
Ruby object model
The Ruby Object Model by Rafael Magana
name name2 n2.ppt
name name2 n
name name2 n2
name name2 n
ppt18
ppt9
ppt7
ppt30
ppt21
ppt2

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Big Data Technologies - Introduction.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
Teaching material agriculture food technology
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Cloud computing and distributed systems.
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Advanced methodologies resolving dimensionality complications for autism neur...
Unlocking AI with Model Context Protocol (MCP)
20250228 LYD VKU AI Blended-Learning.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Big Data Technologies - Introduction.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Modernizing your data center with Dell and AMD
Per capita expenditure prediction using model stacking based on satellite ima...
Understanding_Digital_Forensics_Presentation.pptx
Teaching material agriculture food technology
Chapter 3 Spatial Domain Image Processing.pdf
Machine learning based COVID-19 study performance prediction
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Spectral efficient network and resource selection model in 5G networks
“AI and Expert System Decision Support & Business Intelligence Systems”
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
GamePlan Trading System Review: Professional Trader's Honest Take
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Cloud computing and distributed systems.
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication

Ruby Metaprogramming