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
300
I finally understand instance_eval
Masafumi Okura
February 01, 2020
Tweet
Share
More Decks by Masafumi Okura
See All by Masafumi Okura
RDoc meets YARD
okuramasafumi
1
110
Why doesn't Ruby have Boolean class?
okuramasafumi
0
10
My KaigiEffect at RubyKaigi 2025, part 1
okuramasafumi
1
49
Writing documentation can be fun with plugin system
okuramasafumi
0
200
Alba: Why, How and What's So Interesting
okuramasafumi
0
400
Why did my proposals get rejected?
okuramasafumi
1
710
A suggestion for the future of RDoc
okuramasafumi
1
390
15 JSON serializers for Ruby
okuramasafumi
2
450
Tech Events, Should We See Them from the Outside or the Inside?
okuramasafumi
1
75
Other Decks in Programming
See All in Programming
STUNMESH-go: Wireguard NAT穿隧工具的源起與介紹
tjjh89017
0
380
ゲームの物理
fadis
5
1.5k
学習を成果に繋げるための個人開発の考え方 〜 「学習のための個人開発」のすすめ / personal project for leaning
panda_program
1
110
MCPで実現するAIエージェント駆動のNext.jsアプリデバッグ手法
nyatinte
6
870
Microsoft Orleans, Daprのアクターモデルを使い効率的に開発、デプロイを行うためのSekibanの試行錯誤 / Sekiban: Exploring Efficient Development and Deployment with Microsoft Orleans and Dapr Actor Models
tomohisa
0
210
兎に角、コードレビュー
mitohato14
0
150
エンジニアのための”最低限いい感じ”デザイン入門
shunshobon
0
130
Portapad紹介プレゼンテーション
gotoumakakeru
1
130
tool ディレクティブを導入してみた感想
sgash708
1
150
【第4回】関東Kaggler会「Kaggleは執筆に役立つ」
mipypf
0
790
Google I/O recap web編 大分Web祭り2025
kponda
0
2.9k
Laravel Boost 超入門
fire_arlo
1
120
Featured
See All Featured
RailsConf 2023
tenderlove
30
1.2k
It's Worth the Effort
3n
187
28k
A Tale of Four Properties
chriscoyier
160
23k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Art, The Web, and Tiny UX
lynnandtonic
302
21k
Speed Design
sergeychernyshev
32
1.1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.6k
Designing for humans not robots
tammielis
253
25k
The Language of Interfaces
destraynor
160
25k
Designing for Performance
lara
610
69k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
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ָ͍͠