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.6k
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
140
Crystal
porras
4
320
Enumerator.is_an(Enumerable) [es]
porras
0
150
5 hidden gems of the Ruby Standard Library
porras
2
190
Enumerator is an Enumerable 💃
porras
3
120
Queue and SizedQueue: hidden gems in the Ruby standard library
porras
0
170
Laziness
porras
3
220
Standing on the shoulders of giants
porras
0
210
I used to be a writer (love letter to Ruby)
porras
2
220
Other Decks in Programming
See All in Programming
PHP8.3の機能を振り返る / Review of PHP 8.3 features
seike460
PRO
1
110
Micro Frontends for Java Microservices - Devnexus 2024
mraible
PRO
0
490
Amazon SQSコンシューマー疎結合への旅 - 出張! #DevelopersIO IT技術ブログの中の人が語る勉強会 #3
quiver
0
270
新宿ダンジョンを可視化してみた
satoshi7190
2
250
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
380
Changed Rules: Architectures with Lightweight Stores
manfredsteyer
PRO
0
240
⼤規模⾔語モデルの拡張(RAG)が 終わったかも知れない件について
nearme_tech
23
15k
Elm Form Validation
bkuhlmann
0
510
Apache Hive 4 on Treasure Data
ryukobayashi
0
240
SIMD Parallel Programming with the Vector API
josepaumard
0
160
PHPはいつから死んでいるかの調査
chiroruxx
1
400
Behind VS Code Extensions for JavaScript / TypeScript Linnting and Formatting
unvalley
5
910
Featured
See All Featured
RailsConf 2023
tenderlove
4
540
Statistics for Hackers
jakevdp
789
220k
Intergalactic Javascript Robots from Outer Space
tanoku
266
26k
The Straight Up "How To Draw Better" Workshop
denniskardys
227
130k
4 Signs Your Business is Dying
shpigford
175
21k
BBQ
matthewcrist
80
8.8k
Into the Great Unknown - MozCon
thekraken
10
990
How STYLIGHT went responsive
nonsquared
92
4.8k
Facilitating Awesome Meetings
lara
42
5.6k
Typedesign – Prime Four
hannesfritz
36
2.1k
Designing for Performance
lara
601
67k
Creatively Recalculating Your Daily Design Routine
revolveconf
210
11k
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 :)