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
270
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
480
A suggestion for the future of RDoc
okuramasafumi
1
69
15 JSON serializers for Ruby
okuramasafumi
2
130
Tech Events, Should We See Them from the Outside or the Inside?
okuramasafumi
1
45
Creating gems 101
okuramasafumi
1
85
How NOT to make your DSL terrible
okuramasafumi
0
310
オブジェクトしこう
okuramasafumi
2
220
Debugging Alba
okuramasafumi
1
200
Learning Ruby
okuramasafumi
5
570
Other Decks in Programming
See All in Programming
Zoneless Testing
rainerhahnekamp
0
120
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
160
useSyncExternalStoreを使いまくる
ssssota
6
1.4k
php-conference-japan-2024
tasuku43
0
360
Recoilを剥がしている話
kirik
5
7.2k
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
4
780
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
160
return文におけるstd::moveについて
onihusube
1
1.3k
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
180
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
3
500
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
340
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
360
Featured
See All Featured
GraphQLとの向き合い方2022年版
quramy
44
13k
Speed Design
sergeychernyshev
25
680
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.4k
How GitHub (no longer) Works
holman
311
140k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.9k
Designing Experiences People Love
moore
138
23k
Become a Pro
speakerdeck
PRO
26
5k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
530
It's Worth the Effort
3n
183
28k
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ָ͍͠