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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Sergio Gil
March 25, 2012
Programming
2.8k
11
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Ruby Metaprogramming
Slides from my talk at #codemotion #es (Madrid, March 24th 2012).
http://codemotion.es/
Sergio Gil
March 25, 2012
More Decks by Sergio Gil
See All by Sergio Gil
Understanding Unix pipes with Ruby
porras
0
250
Crystal
porras
4
360
Enumerator.is_an(Enumerable) [es]
porras
0
180
5 hidden gems of the Ruby Standard Library
porras
2
300
Enumerator is an Enumerable 💃
porras
3
160
Queue and SizedQueue: hidden gems in the Ruby standard library
porras
0
220
Laziness
porras
3
250
Standing on the shoulders of giants
porras
0
240
I used to be a writer (love letter to Ruby)
porras
2
260
Other Decks in Programming
See All in Programming
KotlinConf Extended South Korea 2026 Keynote
l2hyunwoo
0
120
Building a Meta Ray-Ban display app
akkeylab
0
180
freee が目指す データ マネジメント戦略 AI-Ready 時代を支える 攻めのガバナンスとは
freee
PRO
0
460
Jindong: Introducing Declarative Haptics in Compose Multiplatform
l2hyunwoo
0
130
AIと壁打ちしながら進めるコスト管理
fufuhu
2
1.5k
go-spidermonkeyでAIエージェントのCode Modeを実装する
syumai
1
700
How I Won Prize Money at a Hackathon Using Codex and Symphony Alpha
yasei_no_otoko
0
120
変わらないものが、変わるものを決める — 意図駆動開発 × イベントソーシング × イミュータブル | What Doesn't Change Decides What Can — IDD × Event Sourcing × Immutability
tomohisa
0
1.8k
Cloudflare is Agents
chimame
0
180
React本体のコードリーディング
high_g_engineer
1
150
ソフトウェアラスタライザ
fadis
1
510
不幸な GC
chencmd
0
640
Featured
See All Featured
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
3
480
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
660
Crafting Experiences
bethany
1
260
30 Presentation Tips
portentint
PRO
1
380
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
64
56k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
440
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
BBQ
matthewcrist
89
10k
The SEO Collaboration Effect
kristinabergwall1
1
530
ラッコキーワード サービス紹介資料
rakko
1
4.5M
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 :)