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
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
230
Crystal
porras
4
360
Enumerator.is_an(Enumerable) [es]
porras
0
170
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
AI駆動開発で崩れていくコードベースを立て直す
kyoko_nr_nr
1
400
Stage 3 Decorators でできること / できないこと / TSKaigi 2026
susisu
1
1.4k
Moments When Things Go Wrong
aurimas
3
130
開発体験を左右するライブラリの API 設計 - GraphQL スキーマ構築ライブラリから考える #tskaigi
izumin5210
2
1.4k
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
7
2.4k
AI駆動開発勉強会 広島支部 第一回勉強会 AI駆動開発概要とワークショップ
hayatoshimiu
0
420
TSKaigi 2026 TypeScriptバックエンドのオブザーバビリティ戦略 — Datadog × NestJSの実践
taiseiyamamotoan
1
210
Spec-Driven Development with AI-Agents: From High-Level Requirements to Working Software
antonarhipov
2
400
New "Type" system on PicoRuby
pocke
1
390
The Arts and Crafts of Work in the AI Era — Toward Mastery in Software Development
kuranuki
1
680
Modding RubyKaigi for Myself
yui_knk
0
830
サーバーレスで作る、動画データ管理基盤
oyasumipants
0
320
Featured
See All Featured
Building AI with AI
inesmontani
PRO
1
1k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.1k
Fireside Chat
paigeccino
42
3.9k
Evolving SEO for Evolving Search Engines
ryanjones
0
210
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.3k
The Invisible Side of Design
smashingmag
302
52k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
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 :)