SlideShare une entreprise Scribd logo
1  sur  48
Volodymyr Byno
github.com/vbyno
volodymyr.byno@gmail.com
domain lead engineer at
Metaprogramming
What?
Writing code that writes code
Writing code that manipulates
language constructs at runtime
• write own DSL
• remove duplications in the code
• enhance basic Ruby
Why?
Object Model
Class is an object, Object is a class
class is a keyword, class is a method
method is a method, Method is a class
Object Model
Class is an Object
Module is an Object
Module Hooks
Class vs Module
Statement vs Method
method
statement
Singleton Method/Class
class A singleton class
Lookup
new?
Methods
unbound method
What does
«everything»
mean?
Everything
is an
object
Open class
NOT Objects
• comments (# comment, =begin =end)
• punctuation marks (, ; ( .)
• statements (if while for defined? yield super)
• operators (&. || && =)
• blocks (do..end, {..})
• methods (not method objects)
Methods are a fundamental part of Ruby's syntax, but they are not values that Ruby
programs can operate on. That is, Ruby's methods are not objects in the way that strings,
numbers, and arrays are. It is possible, however, to obtain a Method object that represents a
given method, and we can invoke methods indirectly through Method objects.
(c) The Ruby Programming Language. D. Flanagan; Y. Matsumoto
operators table
Open Class
Open Class
method already exists
Monkeypatching
Test: Ruby or Rails?
ActiveSupport
3 Ways to Monkey-Patch
why is nobody using refinements
Refinements
• Applicable only for classes
• Works in the current lexical scope
(module/class definition, EOF)
Dynamic
Method
Dynamic Dispatch
Dynamic Method
Comment it out!
Remove Method
Ghost
Method
Ghost Method
respond_to?
Rules:
• Redefine respond_to_missing?(name)
• Call super
respond_to? vs respond_to_missing?
Dynamic vs Ghost
use Dynamic Methods if you can
use Ghost Methods if you have to
Alias
Method
Alias Method
alias vs alias_method
Block
Proc
Lamda
Block
Reccomendation: 1 line - curly braces, multiple
lines - do..end
& operator
yield is not enough:
• pass block to another method
• convert block to Proc
Proc
LambdaProc
returns fom the scope
where it is defined returns from itself
checks the aritydoes not check the arity
Lambdas vs Methods
lambda is evaluated in the scope it is defined,
method is evaluated in the scope of object
Lexical
Scope
Lexical Scope
Flat
Scope
Scope
Gates
scope in ruby
*eval methods
*eval vs *exec methods
def current_user
def user_signed_in?
def authenticate_user!
def user_session
Variable Setter/Getter
Statements / Methods
• BasicObject#__send__
• BasicObject#method_missing
• Kernel#send
• Kernel#public_send
• Kernel#method
• Kernel#class
• Kernel#define_singleton_method
• Kernel#block_given?
• Kernel#respond_to_missing?
• Kernel#respond_to?
• Kernel#eval
• yield
• defined?
• alias
• super
• class
• module
• def
• Module#module_eval
• Module#define_method
• Module#alias_method
• Module#included
• Module#refine
• Module#using
Summary
• Avoid metaprogramming if it is possible!
• Be smart in monkeypatching
• Statements are not methods
• Prefer lambdas over procs
• Use super in method_missing
• Keep scope in mind
• Comment out!
1. Metaprogramming Ruby 2.0
(book)
2. The Ruby Programming Language
(book, Reflection and Metaprogramming section)
3. Ruby Under a Microscope
(Objects and Classes, Constants Lookup)
4. Codebases lessons
Investigate Further

Contenu connexe

Tendances

Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
Edureka!
 

Tendances (16)

Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
 
ITFT - Java
ITFT - JavaITFT - Java
ITFT - Java
 
Internal domain-specific languages
Internal domain-specific languagesInternal domain-specific languages
Internal domain-specific languages
 
Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building Blocks
 
Java essentials for hadoop
Java essentials for hadoopJava essentials for hadoop
Java essentials for hadoop
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
 
2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation Fundamentals2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation Fundamentals
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Java classes in karve nagar pune
Java classes in karve nagar puneJava classes in karve nagar pune
Java classes in karve nagar pune
 
Getting started with typescript and angular 2
Getting started with typescript  and angular 2Getting started with typescript  and angular 2
Getting started with typescript and angular 2
 
[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Typescript
TypescriptTypescript
Typescript
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languages
 

Similaire à Metaprogramming in Ruby

Code reviews
Code reviewsCode reviews
Code reviews
Roger Xia
 
The programming philosophy of jrql
The programming philosophy of jrqlThe programming philosophy of jrql
The programming philosophy of jrql
msg systems ag
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
Alex Borsuk
 
Code reviews
Code reviewsCode reviews
Code reviews
Roger Xia
 
Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Code
rsebbe
 

Similaire à Metaprogramming in Ruby (20)

Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
 
Code reviews
Code reviewsCode reviews
Code reviews
 
Program Synthesis, DreamCoder, and ARC
Program Synthesis, DreamCoder, and ARCProgram Synthesis, DreamCoder, and ARC
Program Synthesis, DreamCoder, and ARC
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean Coding
 
The programming philosophy of jrql
The programming philosophy of jrqlThe programming philosophy of jrql
The programming philosophy of jrql
 
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
Hack in the Box GSEC 2016 - Reverse Engineering Swift ApplicationsHack in the Box GSEC 2016 - Reverse Engineering Swift Applications
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
 
Rootcon X - Reverse Engineering Swift Applications
Rootcon X - Reverse Engineering Swift ApplicationsRootcon X - Reverse Engineering Swift Applications
Rootcon X - Reverse Engineering Swift Applications
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
7 Methods and Functional Programming
7  Methods and Functional Programming7  Methods and Functional Programming
7 Methods and Functional Programming
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Code reviews
Code reviewsCode reviews
Code reviews
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
Csci360 20 (1)
Csci360 20 (1)Csci360 20 (1)
Csci360 20 (1)
 
Csci360 20
Csci360 20Csci360 20
Csci360 20
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Code
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept it
 
Java+8-New+Features.pdf
Java+8-New+Features.pdfJava+8-New+Features.pdf
Java+8-New+Features.pdf
 
DSL's with Groovy
DSL's with GroovyDSL's with Groovy
DSL's with Groovy
 

Dernier

Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 

Dernier (20)

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 

Metaprogramming in Ruby