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
I finally understand instance_eval
Search
Masafumi Okura
February 01, 2020
Programming
1
260
I finally understand instance_eval
Masafumi Okura
February 01, 2020
Tweet
Share
More Decks by Masafumi Okura
See All by Masafumi Okura
Why did my proposals get rejected?
okuramasafumi
1
440
A suggestion for the future of RDoc
okuramasafumi
1
66
15 JSON serializers for Ruby
okuramasafumi
2
110
Tech Events, Should We See Them from the Outside or the Inside?
okuramasafumi
1
39
Creating gems 101
okuramasafumi
1
82
How NOT to make your DSL terrible
okuramasafumi
0
280
オブジェクトしこう
okuramasafumi
2
210
Debugging Alba
okuramasafumi
1
190
Learning Ruby
okuramasafumi
5
560
Other Decks in Programming
See All in Programming
Jakarta EE meets AI
ivargrimstad
0
610
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
Ethereum_.pdf
nekomatu
0
460
Outline View in SwiftUI
1024jp
1
330
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
100
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
330
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
330
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
EventSourcingの理想と現実
wenas
6
2.3k
Featured
See All Featured
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Faster Mobile Websites
deanohume
305
30k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Code Review Best Practice
trishagee
64
17k
Navigating Team Friction
lara
183
14k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
410
Optimising Largest Contentful Paint
csswizardry
33
2.9k
The Language of Interfaces
destraynor
154
24k
Designing for humans not robots
tammielis
250
25k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Optimizing for Happiness
mojombo
376
70k
Transcript
instance_evalશʹཧ ղͨ͠ɿinstance_exec ΕΔͳฤ Speaker: େխ࢙ Event: Burikaigi2020 Date: 2020-02-01
self.inspect • ໊લɿେխ࢙ • ॴଐɿϑϦʔϥϯε • Rubyྺɿ7 • ͖ͳΤσΟλɿVim
self.inspect • ࠷ۙͷ՝ɿ໊ͱϚελεϥΠυͷσβΠ ϯΛͲ͏͢Δ͔ • झຯɿᘖᘣΛᔸΕΔʢࠓϑϨϯνϓϨεʣ • ͖ͳϝιουɿinstance_eval <- ࠓͷ
͡Ίʹ
͜ͷτʔΫʹ͍ͭͯʢϝλʣ • εϥΠυຕɿ29ຕ • ࣌ؒɿ25 • ओͳλʔήοτɿϝλϓϩάϥϛϯάॳ৺ऀ ͔Βதڃऀ • σϞ༗Γ
·͓ͣ͞Β͍
instance_eval ͱ • evalͷ1ͭͰɺҙͷΦϒδΣΫτ ʢBasicObjectؚΉʣͷίϯςΩετͰจࣈྻ ͷධՁ·ͨϒϩοΫͷ࣮ߦ͕Մೳ • DSLͷߏஙͳͲʹଟ༻͞ΕΔ • RubyͷॊೈੑΛࢧ͍͑ͯΔʢݸਓͷݟղʣ
ॳֶऀ͚ิɿධՁͬͯʁ • evalจࣈྻΛRubyͷίʔυʹݟཱ࣮ͯͯ ߦ͢Δ • irb͕͍ͬͯΔ͜ͱɺͱ͍͏ͱΠϝʔδ͠ ͍͔͢ • ධՁӳޠͰevaluationͳͷͰॖΊͯeval
ॳֶऀ͚ิɿϒϩοΫ • ϒϩοΫͷதͷίʔυ͙͢ʹ࣮ߦ͞Εͳ ͍ʢίʔϧόοΫ͞ΕΔɺͱݴ͏ʣ • ϒϩοΫͦͷ··ͰมೖͰ͖ͳ͍ ͕ɺ&blockͳͲͰड͚ͨͷೖՄೳ • ϒϩοΫΛݺͼग़͢ʢcallͱ͔yieldͱ͔ʣͱ ͖ɺҾΛ͢ͱͦΕ͕ϒϩοΫҾʹͳΔ
instance_evalͱଞͷeval Ͳ͏ҧ͏ͷ͔ • evalจࣈྻ͔͠ධՁͰ͖ͳ͍ʢϒϩοΫΛ࣮ ߦͰ͖ͳ͍ʣ • class_evalධՁͷίϯςΩετ͕ҧ͏ • ʢevalͰͳ͍͚Ͳʣinstance_execจࣈྻ ͷධՁ͕Ͱ͖ͳ͍ʢϒϩοΫͷ࣮ߦ͔͠Ͱ͖ͳ
͍ʣɺϒϩοΫҾ͕ͤΔ
σϞͱ͍͏͔؆୯ͳ͍ํ
σϞ͔ΒΘ͔Δ͜ͱ • instance_evalʹͨ͠ϒϩοΫͰselfϨ γʔόΛࢦ͍ͯ͠Δ • ͳͷͰɺϒϩοΫͰselfΛলུ͢ΔͱϨγʔό ͷϝιου͕ݺΔ • self͕লུͰ͖ΔͷͰϓϥΠϕʔτϝιουΠ ϯελϯεมΞΫηε͠์
ެࣜυΩϡϝϯτΛಡΉ
ެࣜυΩϡϝϯτΛಡΉ • https://docs.ruby-lang.org/en/2.7.0/ BasicObject.html#method-i-instance_eval • https://docs.ruby-lang.org/ja/2.7.0/class/ BasicObject.html#I_INSTANCE_EVAL • ༨ஊͳ͕Βɺinstance_evalBasicObjectʹੜ ͍͑ͯΔ
ཁ͢ΔʹԿ͕ॻ͍ͯ͋Δͷ͔ • จࣈྻΛड͚Δͱ͖ͱϒϩοΫΛड͚Δͱ͖ ͱͰڍಈ͕ҧ͏ʢͨΓલʣ • จࣈྻΛड͚Δͱ͖جຊతʹevalͱಉ͡ಈ ͖Λ͢Δ • ϒϩοΫΛड͚Δͱ͖ϒϩοΫΛ࣮ߦ͢Δ ࡍʹϒϩοΫҾʹϨγʔόΛ͢
ॏཁͳ͜ͱ • ϒϩοΫʹҾΛͤͳ͍ • ϒϩοΫʹҾΛͤͳ͍ <-େࣄͳ͜ͱ • DSLΛߏங͢Δࡍʹ͍ͮΒ͍ͱ͖͕͋Δ
࣮ྫ
࣮ྫσϞ͔ΒΘ͔ͬͨ͜ͱ • instance_evalͱinstance_execڍಈ͕ҟͳ Γɺ༻్·ͨҟͳΔ • instance_execड͚ͨϒϩοΫͷҾʹҙͷ ΦϒδΣΫτΛͤΔ • ϒϩοΫΛ࣮ߦ͢Δ͚ͩͳΒinstance_execͷ΄ ͏͕͍͍ͷ͔ʁ
ͪΐͬͱ͓͠Ζ͍
ಛҟʢγϯάϧτϯʣϝιου • Πϯελϯεʹ͔͠ఆٛ͞Ε͍ͯͳ͍ϝιο υ • instance_evalΛ͏ͱେྔੜ͕؆୯ • ࣮ΫϥεϝιουͱݺΕ͍ͯΔͷ͜ ͷҰछ
ಛҟϝιουΛੜͯ͠ΈΔ
instance_evalͰdef͕ݺΔ • Կ͕ى͖͍ͯΔͷͩΖ͏͔ • ͦͷṖΛ໌Β͔ʹ͘͢ɺզʑRubyͷԞ ʢιʔείʔυʣͱཱཱྀͬͨ…
RubyͷιʔείʔυΛಡΉ
ιʔείʔυ͔ΒΘ͔Δ͜ͱ • CݴޠͰॻ͍ͯ͋ΔʢͦΕͦ͏ʣ • vm_eval.cͱ͍͏ϑΝΠϧʹinstance_evalͷ࣮ ͕͋Δ • execution_contextͱ͍͏ͷ͕ॏཁͦ͏
͍͠
RubiniusΛಡΜͰΈΔ
Rubiniusͷinstance_evalΑΓ • RubyͷϒϩοΫ࣮ߦʹ͓͍ͯείʔϓඇৗ ʹॏཁʢʰϝλϓϩάϥϛϯάRubyʱಡ ͏ʣ • CRubyͱಡΈൺΔͱ͍͍͔
·ͱΊ
instance_evalָ͍͠