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
250
I finally understand instance_eval
Masafumi Okura
February 01, 2020
Tweet
Share
More Decks by Masafumi Okura
See All by Masafumi Okura
Creating gems 101
okuramasafumi
1
58
How NOT to make your DSL terrible
okuramasafumi
0
250
オブジェクトしこう
okuramasafumi
2
200
Debugging Alba
okuramasafumi
1
170
Learning Ruby
okuramasafumi
5
530
How to build a conference for both newbies and seasoned devs
okuramasafumi
1
1.7k
Why doesn't Ruby have Boolean class?
okuramasafumi
4
2.1k
Writing Minitest clone in 30 minutes
okuramasafumi
0
470
Developing a Vim plugin with Ruby
okuramasafumi
0
650
Other Decks in Programming
See All in Programming
rbs-inlineを導入してYARDからRBSに移行する
euglena1215
1
290
What is Parser
yui_knk
9
4.1k
Jakarta EE meets AI
ivargrimstad
1
500
Swiftコードバトル必勝法
toshi0383
0
170
GoのIteratorに詳しくなってしまう
inatonix
1
200
実践!難読化ガイド
mitchan
0
200
Modern Angular with the NGRX Signal Store New Rules for Your Architecture @BASTA! 2024 in Mainz
manfredsteyer
PRO
0
140
あなたのアプリ、ログはでてますか?あるいはログをだしてますか? (Funabashi.dev用 軽量版)
uzulla
2
120
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
0
120
Prompt Cachingは本当に効果的なのか検証してみた.pdf
ttnyt8701
0
530
Findy - エンジニア向け会社紹介 / Findy Letter for Engineers
findyinc
4
90k
o1モデルのプロンプトエンジニアリングって?
ktc_wada
0
260
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
248
20k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Learning to Love Humans: Emotional Interface Design
aarron
270
40k
Web development in the modern age
philhawksworth
205
10k
Building an army of robots
kneath
302
42k
Designing on Purpose - Digital PM Summit 2013
jponch
114
6.8k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.4k
Building Better People: How to give real-time feedback that sticks.
wjessup
359
19k
Facilitating Awesome Meetings
lara
49
5.9k
Embracing the Ebb and Flow
colly
83
4.4k
For a Future-Friendly Web
brad_frost
174
9.3k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2k
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ָ͍͠