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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Sergio Gil
March 25, 2012
Programming
2.7k
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
240
Crystal
porras
4
360
Enumerator.is_an(Enumerable) [es]
porras
0
180
5 hidden gems of the Ruby Standard Library
porras
2
290
Enumerator is an Enumerable 💃
porras
3
150
Queue and SizedQueue: hidden gems in the Ruby standard library
porras
0
200
Laziness
porras
3
250
Standing on the shoulders of giants
porras
0
240
I used to be a writer (love letter to Ruby)
porras
2
250
Other Decks in Programming
See All in Programming
えっ!!コードを読まずに開発を!?
hananouchi
0
190
任せる範囲はこう広がった / How the Scope of AI Delegation Has Expanded
nrslib
1
250
共通化で考えるべきは、実装より公開する型だった
codeegg
0
220
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
580
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
170
Contextとはなにか
chiroruxx
1
390
継続モナドとリアクティブプログラミング
yukikurage
3
520
地域 SRE コミュニティ最前線 - ホンマでっかSRE勉強会
tk3fftk
0
220
エンジニアにデザインハーネスを 〜デザインプロセスを規定するためのハーネス〜 / Design harness from an engineer's perspective
rkaga
2
1.4k
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
360
symfony/aiとlaravel/boost
77web
0
120
Prismを使った型安全な暗号化_関数型まつり2026
_fhhmm
0
120
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
55
8.2k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
220
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
460
Bash Introduction
62gerente
615
220k
The Curious Case for Waylosing
cassininazir
1
430
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
330
Raft: Consensus for Rubyists
vanstee
141
7.6k
A Soul's Torment
seathinner
6
3.1k
Odyssey Design
rkendrick25
PRO
2
730
Abbi's Birthday
coloredviolet
3
8.6k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
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 :)