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
160
Crystal
porras
4
330
Enumerator.is_an(Enumerable) [es]
porras
0
160
5 hidden gems of the Ruby Standard Library
porras
2
220
Enumerator is an Enumerable 💃
porras
3
130
Queue and SizedQueue: hidden gems in the Ruby standard library
porras
0
170
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
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
120
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
340
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
770
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
720
Refactor your code - refactor yourself
xosofox
1
260
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
130
RWC 2024 DICOM & ISO/IEC 2022
m_seki
0
210
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
310
fs2-io を試してたらバグを見つけて直した話
chencmd
0
230
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Raft: Consensus for Rubyists
vanstee
137
6.7k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Typedesign – Prime Four
hannesfritz
40
2.4k
GraphQLとの向き合い方2022年版
quramy
44
13k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
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 :)