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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
200
Crystal
porras
4
340
Enumerator.is_an(Enumerable) [es]
porras
0
160
5 hidden gems of the Ruby Standard Library
porras
2
260
Enumerator is an Enumerable 💃
porras
3
140
Queue and SizedQueue: hidden gems in the Ruby standard library
porras
0
190
Laziness
porras
3
240
Standing on the shoulders of giants
porras
0
230
I used to be a writer (love letter to Ruby)
porras
2
240
Other Decks in Programming
See All in Programming
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
400
Automatic Grammar Agreementと Markdown Extended Attributes について
kishikawakatsumi
0
170
re:Invent 2025 トレンドからみる製品開発への AI Agent 活用
yoskoh
0
700
Kotlin Multiplatform Meetup - Compose Multiplatform 외부 의존성 아키텍처 설계부터 운영까지
wisemuji
0
180
CSC307 Lecture 05
javiergs
PRO
0
490
Python札幌 LT資料
t3tra
7
1.1k
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
220
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
610
なぜSQLはAIぽく見えるのか/why does SQL look AI like
florets1
0
400
CSC307 Lecture 06
javiergs
PRO
0
670
カスタマーサクセス業務を変革したヘルススコアの実現と学び
_hummer0724
0
420
メルカリのリーダビリティチームが取り組む、AI時代のスケーラブルな品質文化
cloverrose
2
500
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.5k
Scaling GitHub
holman
464
140k
Art, The Web, and Tiny UX
lynnandtonic
304
21k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.7k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
240
KATA
mclloyd
PRO
34
15k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.1k
Facilitating Awesome Meetings
lara
57
6.7k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.7k
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
Are puppies a ranking factor?
jonoalderson
1
2.6k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
90
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 :)