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
TypeScript 関数型スタイルでバックエンド開発のリアル
naoya
49
16k
Adding Security to Microcontroller Ruby
sylph01
1
160
ソースコードを美しくたもつために ~コードレビューの認知限界を突破し、年間400リリースを達成する~
kotauchisunsun
1
740
Productivity is Messing Around and Having Fun
hollycummins
1
180
GitHub Actionsの痒いところを埋めるサードパーティーランナー
dora1998
2
250
Go製Webアプリケーションのエラーとの向き合い方大全、あるいはやっぱりスタックトレース欲しいやん / Kyoto.go #50
utgwkk
6
2k
RaaP
ksss
0
160
仕様と実装で学ぶOpenTelemetry
drumato
2
980
The Design of Everyday APIs - PyCon 2024
roguelynn
0
180
Sheets API使ってみた
toshi0383
2
180
哲学史とモデリング
tanakahisateru
2
420
RustでAWS Lambda functionをいい感じに書く
taiki45
2
150
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
60
4k
Design by the Numbers
sachag
274
18k
Rebuilding a faster, lazier Slack
samanthasiow
74
8.3k
Building Applications with DynamoDB
mza
88
5.7k
Put a Button on it: Removing Barriers to Going Fast.
kastner
58
3.1k
The Brand Is Dead. Long Live the Brand.
mthomps
49
30k
Designing for Performance
lara
601
67k
Designing for humans not robots
tammielis
247
25k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
245
20k
Creatively Recalculating Your Daily Design Routine
revolveconf
211
11k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
9
1.3k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
123
39k
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 :)