SlideShare a Scribd company logo
Code That Writes Code
metaprogramming
Metaprogramming code-that-writes-code
The “M” word!?
The Boss’ Challenge
1 class Person
2 include CheckedAttributes
3 attr_checked :age do |v|
4 v >= 18
5 end
6 end
7
8 me = Person.new
9 me.age = 39 # OK
10 me.age = 12 # Exception
Requirements
• Applies when class includes CheckedAttributs
Module
• Class Macro: attr_checked
• First Argument as attribute name
• Second Argument can accept a block as
validator of attribute
Before We Start
Reading…
Bill steps
• Kernel method “add_checked_attribute” by
Kernel#eval
• Refactoring: remove Kernel#eval
• Block attribute validator
• Replace method to class macro for all classes
• A Module with attr_checked method, hooks to
class
Kernel#eval
• Spells: String of Code.
• The most Strings of Code feature some kind of
string substitution.
• Ex: ……
1 array = [10, 20]
2 element = 30
3 eval("array << element") # => [10, 20, 30]
eval(string [, binding [, filename [,lineno]]]) → obj
• Evaluates the Ruby expression(s) in String
• If binding is given, which must be a Binding
object, the evaluation is performed in its context.
• If the optional filename and lineno parameters are
present, they will be used when reporting syntax
errors
Ex 1. REST Client
• Its simple HTTP library for sending HTTP request.
1 #Originally, It needs to declare 4 methods
2 def get(path, *args, &b)
3 r[path].get(*args, &b)
4 end
5
6 def set(path, *args, &b)
7 r[path].set(*args, &b)
8 end
9
10 def put(path, *args, &b)
11 r[path].put(*args, &b)
12 end
13
14 def delete(path, *args, &b)
15 r[path].delete(*args, &b)
16 end
Ex 1. REST Client
• Its simple HTTP library for sending HTTP request.
1 # Using "String Of Code"
2 POSSIBLE_VERBS = ['get', 'put', 'post',
3 'delete']
4 POSSIBLE_VERBS.each do |m|
5 #syntax of heredoc
6 eval <<-end_eval
7 def #{m}(path, *args, &b)
8 r[path].#{m}(*args, &b)
9 end
10 end_eval
11 end
Here Document
1 hdoc = <<-hdoc_end
2 This is here document
3 hdoc_end
4
5 puts "Show:#{hdoc}"
6 #=> Show: This is here document
1 puts("Show:" + <<-hdoc_end)
2 This is here document
3 hdoc_end
4 #=> Show: This is here document
1 puts("Show:" + <<-hdoc_end.gsub(/^s+/, ''))
2 This is here document
3 hdoc_end
4 #=> Show:This is here document
Binding objects
• Objects of class Binding encapsulate the
execution context at some particular place in the
code and retain this context for future use.
• Kernel#binding returns the binding object.
1 class MyClass
2 def my_method
3 @x = 1
4 binding
5 end
6 end
7 b = MyClass.new.my_method
8 eval "p @x", b
Strings of Code vs. Blocks
• instance_eval and class_eval also accept String
as argument.
• Which one should you choose?
Avoid String of Code whenever you have an alternative.
The Trouble with eval()
• Difficult to read and modify.
• Won’t report syntax error until the string is
evaluate.
• Security issue: Code injection!
Code Injection
1 def explore_array(method_name)
2 code = "[1,2,3,4].#{method_name}"
3 puts "Evaluating: #{code}"
4 eval code
5 end
6
7 orga_input = "find_index(2)"
8 p explore_array(orga_input) # => 1
9
10 otis_input = "map!(&:next)"
11 p explore_array(otis_input) # => [2, 3, 4, 5]
12
13 sunkai_input = "object_id; Dir.glob('*')"
14 p explore_array(sunkai_input) # => Some
15 sensitive information!!!
Defending
• Dynamic Method & Dynamic Dispatch
• Tainted Objects: object#tainted?
• Safe Levels: $SAFE = 0 ~ 3
• Clean Room: proc()
Step 1 - Checked Attributes
• Kernel method: add_checked_attribute.
• Raise exception if value of attribute is nil or false.
• Source Code
Step 2 - Remove eval()
• class name is variable: Open Class by class_eval
• method name is variable: Dynamic Methods
• instance variable name is variable:
instance_variable_get/instance_variable_set
• Source Code
Step 3 - Block Validator
• Attribute validation according to return value of
block.
• Source Code
Step 4 - Class Marco
• Class marco: attr_checked
• A class marco for all classes: Instance method of
Class/Module.
• Source Code
Hook Methods
• The object model is an eventful place.
• Class inherited
• Module included, prepended and extended
• Instance method added, removed and undefined
• Singleton method added, removed and undefined
Quiz
• Q: undef_method Vs remove_method?
• undef_method: added an exception to that
method.
• remove_method: remove the method at current
class, receiver will try to find it from ancestor.
Quiz
• Q: which one
method is
useless?
1 #Which one method_hook is useless?
2 class Person
3 def self.singleton_method_added(m)
4 p "M1: #{m}"
5 end
6 def self.method_added(m)
7 p "M2: #{m}"
8 end
9 def singleton_method_added(m)
10 p "M3: #{m}"
11 end
12 def method_added(m)
13 p "M4: #{m}"
14 end
15 def hello
16 end
17 def self.hehe
18 end
19 end
20
21 person = Person.new
22 def person.yo
23 end
• method_added
Quiz
• Q: Other approaches for hook method?
• Override method and call super in the end of
method
• Around Alias
Step 5 - Final step
• Declare an CheckedAttributes Module
• Include module as class method: ClassMehtods-
plus-hook.
• Source Code
Epilogue
• Are you smart enough to forget what you have
learned?
• There’s no such thing as metaprogramming. It’s
just programming all the way through
Shuhari (Kanji: 守破離 Hiragana: ゅはり)
• Shu(protect, obey): learning fundamentals, techniques,
heuristics, proverbs.
• Ha(detach, digress): detachment from the illusions of
self.
• Ri(leave, separate): there are no techniques or proverbs,
all moves are natural, becoming one with spirit alone
without clinging to forms; transcending the physical
Not Only in “M” world
• Design Patterns
• TDD
• Scrum
• More…
Ad

Recommended

Test doubles
Test doubles
Francisco Iglesias Gómez
 
Testing the unpredictable
Testing the unpredictable
Francisco Iglesias Gómez
 
Pavel kravchenko obj c runtime
Pavel kravchenko obj c runtime
DneprCiklumEvents
 
Easy mock
Easy mock
Srikrishna k
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL
 
Easy mockppt
Easy mockppt
subha chandra
 
Easy mock
Easy mock
Ramakrishna kapa
 
Object - Oriented Programming Polymorphism
Object - Oriented Programming Polymorphism
Andy Juan Sarango Veliz
 
Java constructors
Java constructors
QUONTRASOLUTIONS
 
19 reflection
19 reflection
dhrubo kayal
 
J query
J query
Ramakrishna kapa
 
Diifeerences In C#
Diifeerences In C#
rohit_gupta_mrt
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
The Ring programming language version 1.5.2 book - Part 70 of 181
The Ring programming language version 1.5.2 book - Part 70 of 181
Mahmoud Samir Fayed
 
Class object method constructors in java
Class object method constructors in java
Raja Sekhar
 
Advance unittest
Advance unittest
Reza Arbabi
 
All about unit testing using (power) mock
All about unit testing using (power) mock
Pranalee Rokde
 
C++Constructors
C++Constructors
Nusrat Gulbarga
 
Java performance
Java performance
Sergey D
 
Constructor oopj
Constructor oopj
ShaishavShah8
 
Constructor in java
Constructor in java
Pavith Gunasekara
 
Easymock Tutorial
Easymock Tutorial
Sbin m
 
Constructor overloading & method overloading
Constructor overloading & method overloading
garishma bhatia
 
Java Methods
Java Methods
Rosmina Joy Cabauatan
 
ITFT-Classes and object in java
ITFT-Classes and object in java
Atul Sehdev
 
Power mock
Power mock
Piyush Mittal
 
Static analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutes
Andrey Karpov
 
Metaprogramming in Ruby
Metaprogramming in Ruby
Nicolò Calcavecchia
 
Metaprogramming in Ruby
Metaprogramming in Ruby
ConFoo
 

More Related Content

What's hot (20)

Java constructors
Java constructors
QUONTRASOLUTIONS
 
19 reflection
19 reflection
dhrubo kayal
 
J query
J query
Ramakrishna kapa
 
Diifeerences In C#
Diifeerences In C#
rohit_gupta_mrt
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
The Ring programming language version 1.5.2 book - Part 70 of 181
The Ring programming language version 1.5.2 book - Part 70 of 181
Mahmoud Samir Fayed
 
Class object method constructors in java
Class object method constructors in java
Raja Sekhar
 
Advance unittest
Advance unittest
Reza Arbabi
 
All about unit testing using (power) mock
All about unit testing using (power) mock
Pranalee Rokde
 
C++Constructors
C++Constructors
Nusrat Gulbarga
 
Java performance
Java performance
Sergey D
 
Constructor oopj
Constructor oopj
ShaishavShah8
 
Constructor in java
Constructor in java
Pavith Gunasekara
 
Easymock Tutorial
Easymock Tutorial
Sbin m
 
Constructor overloading & method overloading
Constructor overloading & method overloading
garishma bhatia
 
Java Methods
Java Methods
Rosmina Joy Cabauatan
 
ITFT-Classes and object in java
ITFT-Classes and object in java
Atul Sehdev
 
Power mock
Power mock
Piyush Mittal
 
Static analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutes
Andrey Karpov
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
The Ring programming language version 1.5.2 book - Part 70 of 181
The Ring programming language version 1.5.2 book - Part 70 of 181
Mahmoud Samir Fayed
 
Class object method constructors in java
Class object method constructors in java
Raja Sekhar
 
Advance unittest
Advance unittest
Reza Arbabi
 
All about unit testing using (power) mock
All about unit testing using (power) mock
Pranalee Rokde
 
Java performance
Java performance
Sergey D
 
Easymock Tutorial
Easymock Tutorial
Sbin m
 
Constructor overloading & method overloading
Constructor overloading & method overloading
garishma bhatia
 
ITFT-Classes and object in java
ITFT-Classes and object in java
Atul Sehdev
 
Static analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutes
Andrey Karpov
 

Similar to Metaprogramming code-that-writes-code (20)

Metaprogramming in Ruby
Metaprogramming in Ruby
Nicolò Calcavecchia
 
Metaprogramming in Ruby
Metaprogramming in Ruby
ConFoo
 
Metaprogramming
Metaprogramming
joshbuddy
 
Introduction to ruby eval
Introduction to ruby eval
Niranjan Sarade
 
Metaprogramming
Metaprogramming
Santiago Pastorino
 
Ruby
Ruby
Kerry Buckley
 
Designing Ruby APIs
Designing Ruby APIs
Wen-Tien Chang
 
Less-Dumb Fuzzing and Ruby Metaprogramming
Less-Dumb Fuzzing and Ruby Metaprogramming
Nephi Johnson
 
Ruby objects
Ruby objects
Reuven Lerner
 
Hijacking Ruby Syntax in Ruby
Hijacking Ruby Syntax in Ruby
SATOSHI TAGOMORI
 
Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)
Christopher Haupt
 
Story for a Ruby on Rails Single Engineer
Story for a Ruby on Rails Single Engineer
TylerJohnson988371
 
Ruby Metaprogramming
Ruby Metaprogramming
Wei Jen Lu
 
Metaprogramming 101
Metaprogramming 101
Nando Vieira
 
Ruby Intro {spection}
Ruby Intro {spection}
Christian KAKESA
 
Deciphering the Ruby Object Model
Deciphering the Ruby Object Model
Karthik Sirasanagandla
 
Ruby basics
Ruby basics
Aditya Tiwari
 
Introduction to the Ruby Object Model
Introduction to the Ruby Object Model
Miki Shiran
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 
Introduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection API
Niranjan Sarade
 
Metaprogramming in Ruby
Metaprogramming in Ruby
ConFoo
 
Metaprogramming
Metaprogramming
joshbuddy
 
Introduction to ruby eval
Introduction to ruby eval
Niranjan Sarade
 
Less-Dumb Fuzzing and Ruby Metaprogramming
Less-Dumb Fuzzing and Ruby Metaprogramming
Nephi Johnson
 
Hijacking Ruby Syntax in Ruby
Hijacking Ruby Syntax in Ruby
SATOSHI TAGOMORI
 
Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)
Christopher Haupt
 
Story for a Ruby on Rails Single Engineer
Story for a Ruby on Rails Single Engineer
TylerJohnson988371
 
Ruby Metaprogramming
Ruby Metaprogramming
Wei Jen Lu
 
Metaprogramming 101
Metaprogramming 101
Nando Vieira
 
Introduction to the Ruby Object Model
Introduction to the Ruby Object Model
Miki Shiran
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 
Introduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection API
Niranjan Sarade
 
Ad

Recently uploaded (20)

Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Ad

Metaprogramming code-that-writes-code

  • 1. Code That Writes Code metaprogramming
  • 4. The Boss’ Challenge 1 class Person 2 include CheckedAttributes 3 attr_checked :age do |v| 4 v >= 18 5 end 6 end 7 8 me = Person.new 9 me.age = 39 # OK 10 me.age = 12 # Exception
  • 5. Requirements • Applies when class includes CheckedAttributs Module • Class Macro: attr_checked • First Argument as attribute name • Second Argument can accept a block as validator of attribute
  • 7. Bill steps • Kernel method “add_checked_attribute” by Kernel#eval • Refactoring: remove Kernel#eval • Block attribute validator • Replace method to class macro for all classes • A Module with attr_checked method, hooks to class
  • 8. Kernel#eval • Spells: String of Code. • The most Strings of Code feature some kind of string substitution. • Ex: …… 1 array = [10, 20] 2 element = 30 3 eval("array << element") # => [10, 20, 30]
  • 9. eval(string [, binding [, filename [,lineno]]]) → obj • Evaluates the Ruby expression(s) in String • If binding is given, which must be a Binding object, the evaluation is performed in its context. • If the optional filename and lineno parameters are present, they will be used when reporting syntax errors
  • 10. Ex 1. REST Client • Its simple HTTP library for sending HTTP request. 1 #Originally, It needs to declare 4 methods 2 def get(path, *args, &b) 3 r[path].get(*args, &b) 4 end 5 6 def set(path, *args, &b) 7 r[path].set(*args, &b) 8 end 9 10 def put(path, *args, &b) 11 r[path].put(*args, &b) 12 end 13 14 def delete(path, *args, &b) 15 r[path].delete(*args, &b) 16 end
  • 11. Ex 1. REST Client • Its simple HTTP library for sending HTTP request. 1 # Using "String Of Code" 2 POSSIBLE_VERBS = ['get', 'put', 'post', 3 'delete'] 4 POSSIBLE_VERBS.each do |m| 5 #syntax of heredoc 6 eval <<-end_eval 7 def #{m}(path, *args, &b) 8 r[path].#{m}(*args, &b) 9 end 10 end_eval 11 end
  • 12. Here Document 1 hdoc = <<-hdoc_end 2 This is here document 3 hdoc_end 4 5 puts "Show:#{hdoc}" 6 #=> Show: This is here document 1 puts("Show:" + <<-hdoc_end) 2 This is here document 3 hdoc_end 4 #=> Show: This is here document 1 puts("Show:" + <<-hdoc_end.gsub(/^s+/, '')) 2 This is here document 3 hdoc_end 4 #=> Show:This is here document
  • 13. Binding objects • Objects of class Binding encapsulate the execution context at some particular place in the code and retain this context for future use. • Kernel#binding returns the binding object. 1 class MyClass 2 def my_method 3 @x = 1 4 binding 5 end 6 end 7 b = MyClass.new.my_method 8 eval "p @x", b
  • 14. Strings of Code vs. Blocks • instance_eval and class_eval also accept String as argument. • Which one should you choose? Avoid String of Code whenever you have an alternative.
  • 15. The Trouble with eval() • Difficult to read and modify. • Won’t report syntax error until the string is evaluate. • Security issue: Code injection!
  • 16. Code Injection 1 def explore_array(method_name) 2 code = "[1,2,3,4].#{method_name}" 3 puts "Evaluating: #{code}" 4 eval code 5 end 6 7 orga_input = "find_index(2)" 8 p explore_array(orga_input) # => 1 9 10 otis_input = "map!(&:next)" 11 p explore_array(otis_input) # => [2, 3, 4, 5] 12 13 sunkai_input = "object_id; Dir.glob('*')" 14 p explore_array(sunkai_input) # => Some 15 sensitive information!!!
  • 17. Defending • Dynamic Method & Dynamic Dispatch • Tainted Objects: object#tainted? • Safe Levels: $SAFE = 0 ~ 3 • Clean Room: proc()
  • 18. Step 1 - Checked Attributes • Kernel method: add_checked_attribute. • Raise exception if value of attribute is nil or false. • Source Code
  • 19. Step 2 - Remove eval() • class name is variable: Open Class by class_eval • method name is variable: Dynamic Methods • instance variable name is variable: instance_variable_get/instance_variable_set • Source Code
  • 20. Step 3 - Block Validator • Attribute validation according to return value of block. • Source Code
  • 21. Step 4 - Class Marco • Class marco: attr_checked • A class marco for all classes: Instance method of Class/Module. • Source Code
  • 22. Hook Methods • The object model is an eventful place. • Class inherited • Module included, prepended and extended • Instance method added, removed and undefined • Singleton method added, removed and undefined
  • 23. Quiz • Q: undef_method Vs remove_method? • undef_method: added an exception to that method. • remove_method: remove the method at current class, receiver will try to find it from ancestor.
  • 24. Quiz • Q: which one method is useless? 1 #Which one method_hook is useless? 2 class Person 3 def self.singleton_method_added(m) 4 p "M1: #{m}" 5 end 6 def self.method_added(m) 7 p "M2: #{m}" 8 end 9 def singleton_method_added(m) 10 p "M3: #{m}" 11 end 12 def method_added(m) 13 p "M4: #{m}" 14 end 15 def hello 16 end 17 def self.hehe 18 end 19 end 20 21 person = Person.new 22 def person.yo 23 end • method_added
  • 25. Quiz • Q: Other approaches for hook method? • Override method and call super in the end of method • Around Alias
  • 26. Step 5 - Final step • Declare an CheckedAttributes Module • Include module as class method: ClassMehtods- plus-hook. • Source Code
  • 27. Epilogue • Are you smart enough to forget what you have learned? • There’s no such thing as metaprogramming. It’s just programming all the way through
  • 28. Shuhari (Kanji: 守破離 Hiragana: ゅはり) • Shu(protect, obey): learning fundamentals, techniques, heuristics, proverbs. • Ha(detach, digress): detachment from the illusions of self. • Ri(leave, separate): there are no techniques or proverbs, all moves are natural, becoming one with spirit alone without clinging to forms; transcending the physical
  • 29. Not Only in “M” world • Design Patterns • TDD • Scrum • More…