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
170
Crystal
porras
4
330
Enumerator.is_an(Enumerable) [es]
porras
0
160
5 hidden gems of the Ruby Standard Library
porras
2
230
Enumerator is an Enumerable 💃
porras
3
130
Queue and SizedQueue: hidden gems in the Ruby standard library
porras
0
180
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
ミリしらMCP勉強会
watany
4
740
サービスレベルを管理してアジャイルを加速しよう!! / slm-accelerate-agility
tomoyakitaura
1
170
Going Structural with Named Tuples
bishabosha
0
200
Code smarter, not harder - How AI Coding Tools Boost Your Productivity | Webinar 2025
danielsogl
0
120
これだけは知っておきたいクラス設計の基礎知識 version 2
masuda220
PRO
24
6.3k
Lambda(Python)の リファクタリングが好きなんです
komakichi
3
130
The Weight of Data: Rethinking Cloud-Native Systems for the Age of AI
hollycummins
0
270
AIコーディングワークフローの試行 〜AIエージェント×ワークフローでの自動化を目指して〜
rkaga
2
3.5k
Agentic Applications with Symfony
el_stoffel
2
280
フロントエンドテストの育て方
quramy
11
3k
趣味全開のAITuber開発
kokushin
0
200
Defying Front-End Inertia: Inertia.js on Rails
skryukov
0
470
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
41
2.2k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
104
19k
Visualization
eitanlees
146
16k
How GitHub (no longer) Works
holman
314
140k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
YesSQL, Process and Tooling at Scale
rocio
172
14k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.3k
Git: the NoSQL Database
bkeepers
PRO
430
65k
RailsConf 2023
tenderlove
30
1.1k
The World Runs on Bad Software
bkeepers
PRO
67
11k
GitHub's CSS Performance
jonrohan
1030
460k
Docker and Python
trallard
44
3.3k
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 :)