Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby Metaprogramming
Search
Sergio Gil
March 25, 2012
Programming
11
2.7k
Ruby Metaprogramming
Slides from my talk at #codemotion #es (Madrid, March 24th 2012).
http://codemotion.es/
Sergio Gil
March 25, 2012
Tweet
Share
More Decks by Sergio Gil
See All by Sergio Gil
Understanding Unix pipes with Ruby
porras
0
150
Crystal
porras
4
330
Enumerator.is_an(Enumerable) [es]
porras
0
160
5 hidden gems of the Ruby Standard Library
porras
2
220
Enumerator is an Enumerable 💃
porras
3
130
Queue and SizedQueue: hidden gems in the Ruby standard library
porras
0
170
Laziness
porras
3
230
Standing on the shoulders of giants
porras
0
210
I used to be a writer (love letter to Ruby)
porras
2
230
Other Decks in Programming
See All in Programming
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
280
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
730
CSC509 Lecture 12
javiergs
PRO
0
140
Tauriでネイティブアプリを作りたい
tsucchinoko
0
360
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
3
450
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
810
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
210
as(型アサーション)を書く前にできること
marokanatani
7
2.4k
CPython 인터프리터 구조 파헤치기 - PyCon Korea 24
kennethanceyer
0
250
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
400
Bash Introduction
62gerente
608
210k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
Side Projects
sachag
452
42k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Scaling GitHub
holman
458
140k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
830
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
How to train your dragon (web standard)
notwaldorf
88
5.7k
The Cult of Friendly URLs
andyhume
78
6k
Transcript
Ruby Metaprogramming Sergio Gil (@porras)
None
require 'excel' require 'sql' Excel.load('file.xls').each do |row| SQL.insert(row) end
None
require 'html' require 'http' page = HTML.load(HTTP.get('http://example.com/')) page.links.each do |link|
HTTP.get(link).save end
class Company def projects HTTP::Request.new('/projects').get end def people HTTP::Request.new('/people').get end
def clients HTTP::Request.new('/clients').get end ... end
Yukihiro Matsumoto ‘Matz’, Ruby creator
None
Metaprogramming
defining metaprogramming
None
None
None
None
Classes Instances Methods Variables Constants Modules
Classes Instances Methods Variables Constants Modules
Memory
Classes Instances Methods Variables Constants Modules
None
>> str = "hola" => "hola"
>> str = "hola" => "hola" >> str.methods => [...,
:reverse, ...]
>> str = "hola" => "hola" >> str.methods => [...,
:reverse, ...] >> def str.reverse "adios" end
>> str = "hola" => "hola" >> str.methods => [...,
:reverse, ...] >> def str.reverse "adios" end >> str.reverse
>> str = "hola" => "hola" >> str.methods => [...,
:reverse, ...] >> def str.reverse "adios" end >> str.reverse => "adios"
Ruby Object Model: The Musical
None
None
None
None
None
It’s all about DRYness
class Company def projects HTTP::Request.new('/projects').get end def people HTTP::Request.new('/people').get end
... end
class Company def projects get('/projects') end def people get('/people') end
... private def get(url) HTTP::Request.new(url).get end end
Is that all?
Let Ruby create your methods for you
class Company [:projects, :people, ...].each do |method| eval %Q{ def
#{method} HTTP::Request.new('/#{method}').get end } end end
class Company [:projects, :people, ...].each do |method| define_method method do
HTTP::Request.new("/#{method}").get end end end
None
It’s all about expresivity
None
module HTTP def get(*methods) methods.each do |method| define_method method do
HTTP::Request.new("/#{method}").get end end end end
module HTTP def get(*methods) methods.each do |method| define_method method do
HTTP::Request.new("/#{method}").get end end end end class Company extend HTTP get :projects, :people, ... end
“I Taw a Putty DSL...”
class Company extend HTTP get :projects, :people, ... end
It’s all about flexibility
class Company def method_missing(method, *args, &blk) HTTP::Request.new("/#{method}").get end end
class Company def method_missing(method, *args, &blk) HTTP::Request.new("/#{method}").get end end
It’s all common sense
metaprogramming == programming
same trade-offs apply
None
None
None
“Cleverness cannot win. The only weapons we have are simplicity
and convention” http://alarmingdevelopment.org/?p=422
Thank you :)