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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
bostonrb
September 09, 2012
Programming
4
900
Dissecting Ruby
bostonrb
September 09, 2012
Tweet
Share
More Decks by bostonrb
See All by bostonrb
What to expect in Rails 4.0
bostonrb
47
11k
Introduction to JRuby
bostonrb
0
290
love your lib directory.pdf
bostonrb
1
360
Other Decks in Programming
See All in Programming
CSC307 Lecture 08
javiergs
PRO
0
670
15年続くIoTサービスのSREエンジニアが挑む分散トレーシング導入
melonps
2
170
CSC307 Lecture 07
javiergs
PRO
0
550
メルカリのリーダビリティチームが取り組む、AI時代のスケーラブルな品質文化
cloverrose
2
510
AI によるインシデント初動調査の自動化を行う AI インシデントコマンダーを作った話
azukiazusa1
1
690
登壇資料を作る時に意識していること #登壇資料_findy
konifar
4
960
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
220
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
170
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
990
Apache Iceberg V3 and migration to V3
tomtanaka
0
150
AIエージェント、”どう作るか”で差は出るか? / AI Agents: Does the "How" Make a Difference?
rkaga
4
2k
副作用をどこに置くか問題:オブジェクト指向で整理する設計判断ツリー
koxya
1
590
Featured
See All Featured
Are puppies a ranking factor?
jonoalderson
1
2.7k
Visualization
eitanlees
150
17k
WCS-LA-2024
lcolladotor
0
440
Darren the Foodie - Storyboard
khoart
PRO
2
2.3k
Unsuck your backbone
ammeep
671
58k
GraphQLの誤解/rethinking-graphql
sonatard
74
11k
Designing Powerful Visuals for Engaging Learning
tmiket
0
220
Designing for Timeless Needs
cassininazir
0
130
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Navigating Weather and Climate Data
rabernat
0
100
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
300
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
420
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