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
2.7k
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
240
Crystal
porras
4
360
Enumerator.is_an(Enumerable) [es]
porras
0
180
5 hidden gems of the Ruby Standard Library
porras
2
290
Enumerator is an Enumerable 💃
porras
3
150
Queue and SizedQueue: hidden gems in the Ruby standard library
porras
0
200
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
250
Other Decks in Programming
See All in Programming
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
240
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
310
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
460
PHPだって関数型したい 〜できること、できないこと〜 / fp-in-php
jsoizo
0
190
ビデオ通話が繋がる0.2秒で何が起きているのか
supurazako
2
140
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
840
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
120
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
1
170
1B+ /day規模のログを管理する技術
broadleaf
0
130
SLOをサービス品質の共通言語にするために 取り組んできたこと
wakana0222
0
480
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
620
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
1.1k
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Embracing the Ebb and Flow
colly
88
5.1k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
420
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
290
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Code Reviewing Like a Champion
maltzj
528
40k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
A designer walks into a library…
pauljervisheath
211
24k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
The SEO Collaboration Effect
kristinabergwall1
1
500
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 :)