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.6k
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
320
Enumerator.is_an(Enumerable) [es]
porras
0
150
5 hidden gems of the Ruby Standard Library
porras
2
210
Enumerator is an Enumerable 💃
porras
3
120
Queue and SizedQueue: hidden gems in the Ruby standard library
porras
0
170
Laziness
porras
3
220
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
初めてのiOS関連GitHub ActionsをMarketplaceに公開するまでの実録
konifar
3
200
RAGの回答精度評価用のQAデータセットを生成AIに作らせた話
kurahara
0
210
僕が思い描くTypeScriptの未来を勝手に先取りする
yukukotani
3
620
プログラマのための音楽入門
cheebow
4
540
Playwrightから始めるVisual Regression Testingのススメ by とっと
totto2727
2
1.8k
詳解UIWindow
natmark
3
2k
ESLint Rule により事業, 技術ドメインに沿った制約と誓約を敷衍させるアプローチのすゝめ
shinyaigeek
1
2.7k
Rustではじめる負荷試験
skanehira
5
1.1k
Ebitengineの1vs1ゲーム WebRTCの活用
ponyo877
0
340
楽しく簡単に!QRコードの読み取り機能を実装しよう
penguinsan_pg
1
190
【TID2024】模擬講義:プログラマと一緒にゲームをデザインしてみよう!
akatsukigames_tech
0
380
オートマトン学習しろ / Do automata learning
makenowjust
3
100
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1029
450k
Faster Mobile Websites
deanohume
304
30k
How to name files
jennybc
75
97k
Art, The Web, and Tiny UX
lynnandtonic
294
20k
Making Projects Easy
brettharned
113
5.8k
10 Git Anti Patterns You Should be Aware of
lemiorhan
653
58k
What's new in Ruby 2.0
geeforr
340
31k
A Tale of Four Properties
chriscoyier
155
22k
The Art of Programming - Codeland 2020
erikaheidi
48
13k
How GitHub (no longer) Works
holman
309
140k
Fantastic passwords and where to find them - at NoRuKo
philnash
47
2.7k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
248
20k
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 :)