View
165
Download
1
Category
Preview:
Citation preview
Metaprogramming Ruby 101502551 資工 3B 蔣彥亭
Monkey Patch
Open Classes
Class, Module, Object
Class
Class, Superclass
Class Wrap-Up
Methods Call"one step to the right, then up."
How about module?
Method Execution
• Receiver
Self
Private in Ruby
Methods
A Duplication Problem
Solutions
1. Dynamic Methods2. Ghost Methods
Call Methods Dynamically
Define Methods Dynamically
Then...Refactor
method_missing()
Overriding method_missing()
Then... Refactor
Closures
Scope Gates
• When does the scope change?
1. Class definitions
2. Module definitions
3. Methods
Scope Gates (cont.)
Flattening the Scope
Flattening the Scope (Sol)
Flattening the Scope (Sol2)
instance_eval()
Clean Rooms
Class Definitions
Current Object & Current Class
• Current Object• self
• Current Class
class_eval()
Class Instance Variables
Class Variables
Singleton Methods
Class Methods, The Truth
Class Macros
Where is the Singleton Method?
Eigenclasses
Eigenclasses and instance_eval()
Eigenclasses and Inheritance
Eigenclasses Wrap-Up
Quiz: Module Trouble
Quiz Solution
Object#extend
Object#extend() is simply a shortcut that includes a module in the receiver’s eigenclass.
Method Aliases
Around Aliases
That's all. Have fun with Ruby Meta!
Reference
• Metaprogramming Ruby: Program Like the Ruby
Pros ( ISBN-13: 978-1934356470 )
Recommended