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
Dissecting Ruby
Search
bostonrb
September 09, 2012
Programming
940
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
310
love your lib directory.pdf
bostonrb
1
390
Other Decks in Programming
See All in Programming
Web Framework Benchmark Battle 2026, Any%
a_matsuda
3
3.1k
Apple Intelligenceとオンデバイス時代の到来
yahir0
0
190
LLM 101
dalinaum
0
440
Engineering Biometric UX Across Android Versions | DroidKnights 2026
ryunen344
0
120
Coding with AI: How to trust the storyteller (AWS Community Day Adria, Ljubljana 2026)
slobodan
0
130
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
300
Crash Auto Pipelines
veronikapj
1
300
SFCで実現する病院・医療システムの動的組み立て式UI
koukimiura
1
160
SODA - FACT BOOK(EN)
sodainc
1
200
AI時代のFlutter品質設計 — アプリ品質を守るQuality Layers
omatty198
0
330
月間100万人のサービス刷新で、 技術選定をHonoに振り切った話
yutobara
8
7.1k
baseline으로 마이그레이션, 리팩토링 안전하게 맡기기
fornewid
1
800
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
273
28k
Discover your Explorer Soul
emna__ayadi
2
1.3k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
470
Six Lessons from altMBA
skipperchong
29
4.6k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
750
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1.1k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
290
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
AI: The stuff that nobody shows you
jnunemaker
PRO
10
1.2k
Building AI with AI
inesmontani
PRO
1
1.4k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
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