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
880
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
260
love your lib directory.pdf
bostonrb
1
340
Other Decks in Programming
See All in Programming
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
4
640
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
170
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
270
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
4
2k
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
0
170
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
推し活の ハイトラフィックに立ち向かう Railsとアーキテクチャ - Kaigi on Rails 2024
falcon8823
6
2.7k
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.3k
Kaigi on Rails 2024 - Rails APIモードのためのシンプルで効果的なCSRF対策 / kaigionrails-2024-csrf
corocn
5
3.8k
CSC509 Lecture 11
javiergs
PRO
0
180
Outline View in SwiftUI
1024jp
1
290
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Facilitating Awesome Meetings
lara
50
6.1k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Designing Experiences People Love
moore
138
23k
YesSQL, Process and Tooling at Scale
rocio
168
14k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Thoughts on Productivity
jonyablonski
67
4.3k
Designing the Hi-DPI Web
ddemaree
280
34k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
We Have a Design System, Now What?
morganepeng
50
7.2k
Agile that works and the tools we love
rasmusluckow
327
21k
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