Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Ruby Metaprogramming
Search
Sergio Gil
March 25, 2012
Programming
2.8k
11
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
260
Crystal
porras
4
380
Enumerator.is_an(Enumerable) [es]
porras
0
190
5 hidden gems of the Ruby Standard Library
porras
2
300
Enumerator is an Enumerable 💃
porras
3
160
Queue and SizedQueue: hidden gems in the Ruby standard library
porras
0
220
Laziness
porras
3
260
Standing on the shoulders of giants
porras
0
240
I used to be a writer (love letter to Ruby)
porras
2
260
Other Decks in Programming
See All in Programming
AI時代のIaC入門 〜なぜインフラをコードで管理したいのか?〜 / Introduction to IaC in the AI Era
akihisaikeda
6
1.5k
学びを記事・スライド・Xへ高速展開する― AI-assisted Learning Loopで回すコンテンツ生成術
ochtum
0
200
エージェントを使い続けるためのメタワーク / Meta-work for keeping using the agent
rkaga
6
2.2k
AIが実装を担う時代の「技術設計」と「チームマネジメント」
kamina_zzz
2
240
graphql-ruby を Spinel で速くしようとした話
mtsmfm
0
140
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
1
300
作れるエンジニアから、価値を届けるエンジニアへ
ryunakayama
1
250
React Native에 현대적인 빌드 도구 더하기: Rollipop 구현기
leegeunhyeok
0
180
Web3Dライブラリを作ってVue Custom Rendererの仕組みを理解しよう / lets learn vue custom renderer by making 3d library
drumath2237
1
130
LLM 101: Exploring LLMs, Starting with Tokens.
dalinaum
0
100
Next.js: The final boss of Web Performance
guaca
0
390
SODA - FACT BOOK(EN)
sodainc
1
220
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
433
67k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.6k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
68
60k
Building Adaptive Systems
keathley
44
3.3k
Scaling GitHub
holman
464
140k
How Software Deployment tools have changed in the past 20 years
geshan
3
35k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
930
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.9k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
Statistics for Hackers
jakevdp
799
230k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
340
How to Ace a Technical Interview
jacobian
280
24k
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 :)