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
Dissecting Ruby
Search
bostonrb
September 09, 2012
Programming
920
4
Share
Dissecting Ruby
bostonrb
September 09, 2012
More Decks by bostonrb
See All by bostonrb
What to expect in Rails 4.0
bostonrb
47
11k
Introduction to JRuby
bostonrb
0
300
love your lib directory.pdf
bostonrb
1
370
Other Decks in Programming
See All in Programming
サーバーレスで作る、動画データ管理基盤
oyasumipants
0
290
今さら聞けないCancellationToken
htkym
0
200
[KCD Czech] eBPF Meets the GPU: Future of AI Infra Observability
doniacld
0
110
Agentic UI beyond Chats Architecture Patterns & Open Standards @ngMunich 05/2026
manfredsteyer
PRO
0
160
バックエンドにElysiaJSを採用して気付いた、良い点・悪い点
wanko_it
1
180
CLIであることを活かしたGitHub Copilot CLI活用術 / GitHub Copilot CLI Pro Tips & Tricks
nao_mk2
1
1.1k
AIとRubyの静的型付け
ukin0k0
0
190
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
450
oxlintはeslint/typescript-eslintを置き換えられるのか
shomafujita
2
270
Talking to terminals (and how they talk back) (KotlinConf 2026)
jakewharton
PRO
1
150
TypeSpec で繋ぐ複数プロダクトの型安全
maroon8021
1
260
関係性から理解する"同一性"の型用語たち
pvcresin
2
590
Featured
See All Featured
The Curious Case for Waylosing
cassininazir
1
360
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
310
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
65
55k
How GitHub (no longer) Works
holman
316
150k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
410
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Side Projects
sachag
455
43k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
270
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.3k
Documentation Writing (for coders)
carmenintech
77
5.3k
Transcript
Dissecting a Ruby Block Pat Shaughnessy http://patshaughnessy.net BostonRB Sept. 11,
2012
Why learn Ruby internals?
10.times do |n| puts n end
None
Blocks: Closures in Ruby Closures and Metaprogramming
rb_block_t ??
Calling a block
10.times do str = "The quick brown fox..." puts str
end
rb_block_t iseq putstring "The quick brown fox… " setdynamic str,
0 putself getdynamic str, 0 send :puts, 1 leave
Referencing variables from the parent scope
str = "The quick brown fox" 10.times do str2 =
"jumps over the lazy dog." puts "#{str} #{str2}" end
str = "The quick brown fox"
YARV internal stack locals: str rb_control_frame_t LFP
str = "The quick brown fox" 10.times do ...
YARV internal stack locals: str rb_block_t iseq DFP rb_control_frame_t LFP
... do str2 = "jumps over the lazy dog." puts
"#{str} #{str2}" end
rb_block_t iseq DFP YARV internal stack locals: str locals: str2
DFP rb_control_frame_t
rb_block_t iseq DFP putstring "jumps over the lazy dog." setdynamic
str, 0 putself getdynamic str2, 1 tostring putstring " " getdynamic str, 0 tostring concatstrings 3 send :puts, 1, nil, 8, <ic:0> leave YARV internal stack locals: str
None
“In computer science, a closure (also lexical closure or function
closure) is a function or reference to a function together with a referencing environment...” Sussman and Steele. Scheme: An interpreter for extended lambda calculus
Creating and calling a lambda
None
def display_message str = "The quick brown fox" lambda do
str2 = "jumps over the lazy dog" puts "#{str} #{str2}" end end display_message.call
def display_message str = "The quick brown fox" ... end
YARV internal stack locals: str rb_control_frame_t LFP
str = "The quick brown fox" lambda do ... end
str rb_control_frame_t LFP rb_env_t env rb_proc_t rb_block_t iseq DFP envval
is_lambda Stack Heap str
display_message.call
locals: str2 DFP rb_control_frame_t rb_env_t env rb_proc_t rb_block_t iseq DFP
envval is_lambda Stack Heap str
def display_message str = "The quick brown fox" lambda do
str2 = "jumps over the lazy dog" puts "#{str} #{str2}" end end display_message.call
Blocks: Closures in Ruby Closures and Metaprogramming
Using a closure to define a method
class Quote def initialize @str = "The quick brown fox..."
end def display_message puts @str end end
class Quote def initialize @str = "The quick brown fox..."
end define_method :display_message do puts @str end end
class Quote def initialize @str = "The quick brown fox"
end end str2 = "jumps over the lazy dog." Quote.send(:define_method, :display_message) do puts "#{@str} #{str2}" end
eval and binding
str = "puts" str += " 2" str += "
+" str += " 2" eval(str) => 4
class Quote def initialize @str = "The quick brown fox..."
end def get_binding binding end end
obj = Quote.new
rb_control_frame_t LFP self RObject ivptr klass @str etc...
obj = Quote.new eval('puts @str', obj.get_binding)
rb_control_frame_t LFP self rb_binding_t filename line_no env rb_env_t rb_block_t iseq
DFP env self Stack Heap RObject ivptr klass @str etc...
putstring "The quick brown fox… " setdynamic str, 0 putself
getdynamic str, 0 send :puts, 1 leave YARV internal stack locals: str RObject ivptr klass rb_block_t DFP self iseq
instance_eval
class Quote def initialize @str = "The quick brown fox"
end end
str2 = "jumps over the lazy dog." obj = Quote.new
obj.instance_eval do puts "#{@str} #{str2}" end
instance_eval and singleton classes
class Quote def initialize @str = "The quick brown fox..."
end end
obj = Quote.new obj.instance_eval do def display_message puts @str end
end
obj.display_message The quick brown fox jumps over the lazy dog.
Quote.new.display_message ...undefined method `display_message' for #<Quote:0x007fdf789504e8> (NoMethodError)
RClass super RClass super Object (class) Kernel (module) Quote (class)
RClass super RClass super BasicObject (class) RObject klass obj (object)
etc... RClass super RClass super Object (class) obj (object) RObject
klass SomeClass (class) RClass super #<Class:#<Quote:0x007f9ed9150c28>> (singleton)
http://patshaughnessy.net/ruby-under-a-microscope