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
4
890
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
270
love your lib directory.pdf
bostonrb
1
340
Other Decks in Programming
See All in Programming
SpringBoot3.4の構造化ログ #kanjava
irof
2
1k
コミュニティ駆動 AWS CDK ライブラリ「Open Constructs Library」 / community-cdk-library
gotok365
2
130
データベースのオペレーターであるCloudNativePGがStatefulSetを使わない理由に迫る
nnaka2992
0
150
『品質』という言葉が嫌いな理由
korimu
0
160
一休.com のログイン体験を支える技術 〜Web Components x Vue.js 活用事例と最適化について〜
atsumim
0
490
Open source software: how to live long and go far
gaelvaroquaux
0
630
動作確認やテストで漏れがちな観点3選
starfish719
6
1k
XStateを用いた堅牢なReact Components設計~複雑なClient Stateをシンプルに~ @React Tokyo ミートアップ #2
kfurusho
1
910
sappoRo.R #12 初心者セッション
kosugitti
0
250
富山発の個人開発サービスで日本中の学校の業務を改善した話
krpk1900
4
390
ARA Ansible for the teams
kksat
0
150
AWS Organizations で実現する、 マルチ AWS アカウントのルートユーザー管理からの脱却
atpons
0
150
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
137
6.8k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.3k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.2k
Measuring & Analyzing Core Web Vitals
bluesmoon
6
240
How STYLIGHT went responsive
nonsquared
98
5.4k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.5k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
40
2k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
Why Our Code Smells
bkeepers
PRO
336
57k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Code Review Best Practice
trishagee
67
18k
Statistics for Hackers
jakevdp
797
220k
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