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
推しメソッドsource_locationのしくみを探る - はじめてRubyのコードを読んでみた
Search
nobu09
February 28, 2025
Programming
3
440
推しメソッドsource_locationのしくみを探る - はじめてRubyのコードを読んでみた
TokyoWomen.rb #1(2025.03.01)の登壇資料です
https://tokyowomenrb.connpass.com/event/342573/
nobu09
February 28, 2025
Tweet
Share
Other Decks in Programming
See All in Programming
アプリを起動せずにアプリを開発して品質と生産性を上げる
ishkawa
0
280
SQL Server ベクトル検索
odashinsuke
0
110
自分のために作ったアプリが、グローバルに使われるまで / Indie App Development Lunch LT
pixyzehn
1
120
Firebase Dynamic Linksの代替手段を自作する / Create your own Firebase Dynamic Links alternative
kubode
0
180
CQRS+ES勉強会#1
rechellatek
0
400
Windows版PHPのビルド手順とPHP 8.4における変更点
matsuo_atsushi
0
370
AHC 044 混合整数計画ソルバー解法
kiri8128
0
300
小さく段階的リリースすることで深夜メンテを回避する
mkmk884
2
130
GDG Super.init(version=6) - From Where to Wear : 모바일 개발자가 워치에서 발견한 인사이트
haeti2
0
560
MCP世界への招待: AIエンジニアが創る次世代エージェント連携の世界
gunta
2
570
複雑なフォームと複雑な状態管理にどう向き合うか / #newt_techtalk vol. 15
izumin5210
4
3k
CRE Meetup!ユーザー信頼性を支えるエンジニアリング実践例の発表資料です
tmnb
0
350
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
48
7.6k
Become a Pro
speakerdeck
PRO
27
5.2k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Music & Morning Musume
bryan
46
6.4k
Navigating Team Friction
lara
184
15k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Why You Should Never Use an ORM
jnunemaker
PRO
55
9.3k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.8k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
102
18k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
2.9k
Visualization
eitanlees
146
16k
BBQ
matthewcrist
88
9.5k
Transcript
͡Ίͯ Ruby ͷίʔυΛಡΜͰΈͨ 💎 ਪ͠ϝιου source_location ͷ͘͠ΈΛ୳Δ nobu09 2025.03.01 TokyoWomen.rb
#1
ࣗݾհ • WebΤϯδχΞ • Ruby Rails ͰϓϩάϥϜΛॻ͘ͷ͕͖ • εΫϥϜͰ͍͍ײ͡ʹ։ൃ͢Δ͜ͱʹڵຯؔ৺
• খֶ5ੜͷ͜Ͳ͕͍·͢ • Rails Girls Tokyo 15th ίʔν • SIer → ϢϏϨδ → GMOϖύϘ → ZAICO nobu09 / ࣰా ৳ࢠ @nobu09_ nobu09
TokyoWomen.rb ͞Μͱࢲ • લͷΠϕϯτ TokyoGirls.rb Meetup vol.2 ʹࢀՃ • ͱָܹͯ͘͠Λड͚ͨ
🙌 • ࣌ɺΔͼ·ʹΠϕϯτϨϙʔτ Λॻ͔͍͍ͤͯͨͩͨ 🥲 • ࠓొஃͯ͠Έ͍ͨͱࢥ͍ɺϓ ϩϙʔβϧΛఏग़ https://magazine.rubyist.net/articles/0061/0061-TokyoGirlsrb02Report.html
ࠓ͓͢Δ͜ͱ ࣗͷ͖ͳ Ruby ͷϝιου͕ ͲΜͳ͘͠ΈͰಈ͍͍ͯΔͷ͔Γͨͯ͘ ͡Ίͯ Ruby ͷίʔυΛ ಡΜͰΈͨ
͠༰ʹޡΓ͕͋Ε ͝ࢦఠ͍͚ͨͩͨΒ͏Ε͍͠Ͱ͢ 😌
1. ͖ͳ Ruby ͷϝιου 💎
͋ͳͨͷ͖ͳ Ruby ͷϝιου ͳΜͰ͔͢ʁ
͋ͳͨͷ͖ͳ Ruby ͷϝιου ͳΜͰ͔͢ʁ map tap reject f latten compact
dig each reduce caller presence gsub split times partition include?
Method#source_location 💓
source_location ͷศརͳͱ͜Ζᶃ ϝιου͕ఆٛ͞Ε͍ͯΔιʔείʔυͷϑΝΠϧͱߦΛڭ͑ͯ ͘ΕΔʂ https://docs.ruby-lang.org/ja/latest/method/Method/i/ source_location.html my_method ϝιου my_class.rb ϑΝΠϧͷ2ߦʹ
ఆٛ͞Ε͍ͯΔ 💡 irb r a ils consoleͰ ௐΔ͜ͱ͕Ͱ͖Δ 👈
1. MyClass.new → MyClass ͷ৽͍͠Πϯελϯε࡞ 2. method(:my_method) ɹˠ MyClassΠϯελϯεͷ my_method
Λ Method ΦϒδΣΫτͱͯ͠औಘ ɹˠ method Kernel Ϟδϡʔϧʹఆٛ͞Ε͍ͯΔͨΊͯ͢ͷΦϒδΣΫτͰར༻Ͱ͖Δ ✍ MyClass.new.method(:my_method) ԿΛ͍ͯ͠Δʁ https://docs.ruby-lang.org/ja/latest/class/Object.html#I_METHOD MyClass ͷΠϯελϯεͷ my_method ϝιουΛ Method ΦϒδΣΫτͱͯ͠औಘʂ
source_location ͷศརͳͱ͜Ζᶄ Ͳ͜Ͱఆٛ͞Ε͔͔ͨΓʹ͍͘ಈతʹఆٛ͞Εͨϝιου ʢdefine_methodʣ gem ͷϝιουͷఆ͙ٛ͢ಛఆͰ͖Δ 🕵 ϝιου໊Ͱจࣈྻݕࡧͯ͠ ϝιουఆ͕ٛग़ͯ͜ͳ͍ɾɾ ॲཧ༰͕Θ͔Βͳ͍ɾɾ
ྫ͑͜Μͳϝιουݺͼग़͠ͷఆٛݩΛ୳͍ͯ͠Δͱ͖ˣ
ͦΜͳͱ͖ source_location Λ͓͏ ͜ͷίʔυͰ3ͭͷϝιου͕ಈతʹ ఆٛ͞Ε͍ͯΔ 📝 • hello_ a lice
• hello_bob • hello_ch a rlie ͜ͷϞδϡʔϧͷ͜ͷߦͰ de fi ne_methodͰ ಈతʹϝιουఆٛ͞Εͯͨʂ 👀 ͔ͩΒจࣈྻݕࡧͰग़ͯ͜ͳ͔ͬͨͷ͔
✍ define_method https://docs.ruby-lang.org/ja/latest/method/Module/i/define_method.html define_method ɺϝιου໊ΛಈతʹܾΊͯఆٛͰ͖Δ ϝιου ॊೈʹΫϥεϞδϡʔϧͷϝιουఆ͕ٛͰ͖Δ ࣅͨΑ͏ͳϝιουΛෳఆٛ͢Δͱ͖ͳͲʹศར จࣈྻల։Ͱϝιου໊ΛܾΊ͍ͯΔ ϝιουͷॲཧ༰ϒϩοΫͷதʹॻ͘
ʢdo ʙ endʣ
Ͳ͏ͬͯϝιουͷఆٛॴΛ औಘ͍ͯ͠ΔΜͩΖ͏ 💭
💡 ௐͯΈΑ͏
2. ͬͯΈͨ͜ͱ 🔍
࠷ॳͷน Ͳ͔͜Β ௐΕ͍͍͔ Θ͔Βͳ͍… 😐 Ch a tGPTʹ ௐํΛ ฉ͍ͯΈΑ͏
💡
ChatGPT ʹௐํΛฉ͍ͯΈΔ Ruby ͷ source_loc a tion ͱ͍͏ϝιου͕ ศརͰ͖Ͱ͢ɻ ͲΜͳ͘͠Έͳͷ͔Γ͍ͨͰ͢ɻ
Ͳ͏ͬͯௐͨΒྑ͍Ͱ͔͢ɻ RubyͷιʔείʔυΛಡΉ͜ͱͰɺsource_loc a tion ͷ ࣮ͦͷཪଆΛཧղͰ͖·͢ɻ 1. RubyͷιʔείʔυΛऔಘ͢Δ … ͬͯΈͨ͜ͱ ᶃ
ChatGPT ͕͘ΕͨΞυόΠε 1. Ruby ͷιʔείʔυΛऔಘ͢Δ 2. source_location ͷ࣮Λ୳͢ 3. ؔͷ༰ΛಡΉ
4. C ߏମΛಡΉ 5. ඞཁʹԠͯ͡ Ruby ͷެࣜ υΩϡϝϯτΛಡΉ
ͳΔ΄Ͳ Θ͔ΒΜ ͰɺͲ͏Β C ݴޠͰ ॻ͔Ε͍ͯΔΒ͍͠
ͬͯΈͨ͜ͱ ᶄ GitHub ͷ Ruby ͷϦϙδτϦ͔ΒιʔείʔυΛ clone https://github.com/ruby/ruby
git clone ʹ1͔͔ͬͨ 😲 ͚ͬ͜͏େ͖͍ϦϙδτϦɺͳΜ͔ͩײ֒ਂ͍… 🥺
ίʔυΛͲ͔͜Β ಡΜͰ͍͚͍͍ͷ͔ ϦϙδτϦେ͖͍ɾɾ Cݴޠɾɾɾ
ͬͯΈͨ͜ͱᶅ GitHub Copilot Chat ͱ ChatGPT ʹฉ͖ͳ͕Β ίʔυΛಡΈਐΊΔ
grep Ͱ source_location Λ͕͢͞ ͍ͬͺ͍ʂ😲 const_source_loc a rb_source_loc a test
spec doc ϑΥϧμ ݟͳͯ͘ྑͦ͞͏
proc.c ͜ͷ͋ͨΓͰʁ👀 https://github.com/ruby/ruby/blob/master/proc.c
ίʔυΛ ோΊͯΈΑ͏
rb_de f ine_methodͷهड़͕ ෳՕॴ͋Δ 👀
rb_de f ine_method ɺRuby ͷ C ֦ுϥΠϒϥϦͰ༻͞ΕΔ ؔͰɺRuby ΫϥεʹϝιουΛఆٛ͢ΔͨΊʹ༻͞Ε·͢ ͜ͷؔɺࢦఆ͞ΕͨΫϥεʹ৽͍͠ϝιουΛՃ͠·͢
‼
💡 rb_define_method ͜ͷؔͰɺRuby ͷΠϯελϯεϝιουΛఆ͍ٛͯ͠Δʂ Ruby ͷΫϥεϞδϡʔϧʹΠϯελϯεϝιουΛఆٛ͢ΔͨΊͷ C ݴޠͷؔͰɺRuby ͷ def
ʹ૬͢Δ https://docs.ruby-lang.org/ja/latest/function/rb_define_method.html • klass: ϝιουΛఆٛ͢ΔΫϥε • name: ϝιουͷ໊લ • func: ϝιουͷ࣮Λఏڙ͢Δ C ؔͷϙΠϯλ • argc: ϝιου͕ड͚औΔҾͷʢՄมҾͷ߹ -1 Λࢦఆʣ
💡 source_location Method ͚ͩ͡Όͳ͍ Proc, UnboundMethod, Binding Ϋϥεʹఆ͕ٛ͋Δ
Ϋϥε໊ Ϋϥεͷׂ source_loc a tionͰ औಘͰ͖Δใ Proc ϒϩοΫ Proc Λද͢
ͦͷ Proc ͕ఆٛ͞Εͨॴ UnboundMethod ΫϥεͷΠϯελϯεϝιου ʢະόΠϯυʣΛද͢ ͦͷϝιου͕ఆٛ͞Εͨॴ Binding มͷείʔϓ࣮ߦίϯςΩε τΛද͢ ͦͷ binding ͕औಘ͞Εͨॴ 💡 source_location Method ͚ͩ͡Όͳ͍ Proc, UnboundMethod, Binding Ϋϥεʹఆ͕ٛ͋Δ
✍ Proc#source_location https://docs.ruby-lang.org/ja/latest/method/Proc/i/source_location.html Proc͕ఆٛ͞ΕͨॴΛฦ͢
✍ UnboundMethod#source_location https://docs.ruby-lang.org/ja/latest/class/UnboundMethod.html#I_SOURCE_LOCATION ΫϥεͷΠϯελϯεϝιουͷఆٛॴΛಛఆ͢Δ UnboundMethod ɺϨγʔόΛ࣋ͨͳ͍ϝιουΛද͢Ϋϥε ಛఆͷΠϯελϯεʹόΠϯυ͞Ε͍ͯͳ͍ঢ়ଶ
✍ Binding#source_location https://docs.ruby-lang.org/ja/latest/class/Binding.html#I_SOURCE_LOCATION ͦͷ binding ͕औಘ͞ΕͨॴΛฦ͢ Binding Λ͏ͱɺݱࡏͷείʔϓΛΩϟϓνϟ͠ɺޙ͔Βૢ࡞ɾࢀরͰ͖Δ eval ͰείʔϓΛࢦఆͯ͠ૢ࡞ͨ͠Γɺ
pry ͰσόοάͰ͖Δ
rb_define_method ͔Βݟ͍ͯ͘🔍 Method#source_location C ݴޠͷ rb_method_location ؔΛ ݺͼग़͍ͯ͠Δ
rb_method_location ͜͜ 🚩 https://github.com/ruby/ruby/blob/c27f433d7c0ddb13501f7e19a0158da3aec1577c/proc.c#L3045-L3057
VALUE ͬͯͳʹʁ ΓҾͷVALUEͬͯ Ͳ͏͍͏ҙຯͳΜͩΖ͏ʁ 👀
💡 VALUE ͱ • CݴޠͰ Ruby ͷͯ͢ͷΦϒδΣΫτΛද͢ܕ • RubyͰɺͯ͢ͷΦϒδΣΫτ͕ VALUE
ͷܕΛ ͬͯཧ͞Ε͍ͯΔ https://github.com/ruby/ruby/blob/master/include/ruby/internal/value.h VALUE Cݴޠͷ uintptr_t ܕͱͯ͠ఆٛ͞Ε͍ͯΔ
rb_method_location Ҿͱͯ͠ɺRuby ͷΦϒδΣΫτͰ͋Δ VALUE ܕͷ method Λड͚औ͍ͬͯΔ
࣍ method_def_location ΛݟΔ 🔍
method_def_location ͜͜ 🚩 https://github.com/ruby/ruby/blob/ c27f433d7c0ddb13501f7e19a0158da3aec1577c/proc.c#L3027-L3036
method_def_location ΛಡΈղ͘ᶃ ͜ͷҾɺRubyͷϝιουఆٛΛද͢ߏମ rb_method_de f inition_t ͷϙΠϯλ ϝιουఆٛͷৄࡉใʢϝιουͷछྨϝιουͷຊମͳͲʣΛؚ ΜͰ͍Δߏମ https://github.com/ruby/ruby/blob/dfc25204235079e23eadf9e0ba860c1ebcb14325/method.h#L179-L198
method_def_location ΛಡΈղ͘ᶄ ϝιουͷछྨ͕ҎԼͷ߹ɺҾͷrb_method_de f inition_t ߏମͷதʹҐஔใ͕ଘࡏ͢Δ ߹ͦͷҐஔใΛෳͯ͠ฦ͠ɺҐஔใ͕ߏମʹଘࡏ͠ͳ͍߹ nil Λฦ͢ ɾVM_METHOD_TYPE_ATTRSET
ɹɹ ଐੑͷ setter ϝιουʢ a ttr_ a ccessor a ttr_writer Ͱఆٛ͞Εͨ setterʣ ɾVM_METHOD_TYPE_IVAR ɹɹΠϯελϯεมΛฦ͢ getter ϝιου
method_def_location ΛಡΈղ͘ᶅ ͦΕҎ֎ͷ௨ৗͷϝιουͷ߹ ɾmethod_def_iseq ؔΛ༻ͯ͠ϝιουఆٛͷ໋ྩγʔέϯεʢISeqʣΛऔಘ ɾͦͷҐஔใΛ iseq_loc a tion ؔΛͬͯฦ͢
💡Ruby ͷ໋ྩγʔέϯεʢISeqʣͱ • Ruby ͷίʔυ࣮ߦ͞ΕͣɺҰόΠτίʔυʹม͞ΕΔ • ͜ͷόΠτίʔυ໋͕ྩγʔέϯεʢISeq / Instruction Sequenceʣ
• ISeq ϝιουఆٛ࣌εΫϦϓτͷղੳ࣌ʹ࡞ΒΕΔ • Ruby VM ISeq Λղऍɾ࣮ߦ͢Δ͜ͱͰϓϩάϥϜΛಈ͔͢ Ruby VM Ruby ͷίʔυ ໋ྩγʔέϯεʢISeqʣ ม ղऍɾ࣮ߦ
໋ྩγʔέϯεΛݟͯΈΔ 🔍 https://docs.ruby-lang.org/ja/latest/class/RubyVM=3a=3aInstructionSequence.html RubyVM::InstructionSequence Ϋϥε ໋ྩγʔέϯεʢISeqʣΛද͢Ϋϥε disassemble ϝιουͰਓ͕ؒಡΊΔจࣈྻʹ มͯ͘͠ΕΔ
໋ྩγʔέϯεΛݟͯΈΔ 🔍 ISeqͷதʹϝιου͕ఆٛ͞ΕͨϑΝΠϧ ߦ൪߸Λ͍࣋ͬͯΔ source_locationɺISeq ͷதʹ͋Δ ϝιουͷఆٛॴͷใΛฦ͍ͯ͠Δ
💡໋ྩγʔέϯεͱ source_location • Ruby ͷϝιουఆٛ࣌ʹ໋ྩγʔέϯεʢISeqʣʹ ม͞ΕΔ • ISeq ʹɺϝιουͷఆٛϑΝΠϧߦ൪߸ͳͲͷ ใؚ͕·Ε͍ͯΔ
• ௨ৗͷϝιουͷ߹ɺsource_location ISeq ͔Βఆ ٛॴΛऔಘͯ͠ฦ͢ 💡
3. Θ͔ͬͨ͜ͱ 💡
💡source_location ͕ฦ͢ใ ϝιουͷछྨ ϝιουఆٛॴΛ͍࣋ͬͯΔॴ source_loc a tion ͷॲཧ ௨ৗͷϝιου ໋ྩγʔέϯεʢISeqʣ
໋ྩγʔέϯε͔Βϝιουఆٛ ॴΛฦ͢ ଐੑΞΫηε༻ͷ ϝιου Ruby ͷϝιουͷఆٛใΛ֨ೲ͢ Δߏମʢrb_method_de f inition_tʣ ߏମ͔ΒϝιουఆٛॴΛฦ͢
• Method#source_location ɺϝιουͷఆٛใΛ ࣮ߦ࣌ʹಈతʹղੳ͢ΔͷͰͳ͘ɺ͋Β͔͡Ίϝ ιουఆٛ࣌ʹੜ͞Ε͍ͯΔใΛฦ͍ͯͨ͠ 💡 • Ruby Ͱɺϝιου͕Ͳ͜Ͱఆٛ͞Εͨͷ͔Λޙ ͔Β؆୯ʹΔ͜ͱ͕Ͱ͖Δ͘͠Έ͕͋Δ
͍৺ख৮Γ͕͍͍ Method#source_loc a tion͕ ͬͺΓ͖ʂ
4. ;Γ͔͑ͬͯ 🔭
͡Ίͯ Ruby ͷίʔυΛͷ͍ͧͯΈͨ AI ʹͯ͠Βͬͨ ʮಡΜͩʯͱ͍͏ΑΓʮோΊΔʯʹ͍ۙମݧ ཧղͰ͖ͳ͍͜ͱଟ͍͚ΕͲ ৽͍͠ؾ͖ͮͨ͘͞Μ
Θ͔Βͳ͍͚Ͳͨͷ͍͠ Θ͔Βͳ͍͔Βͨͷ͍͠ 🙌
source_location Λ͖͔͚ͬʹ ·ͨҧ͏ Ruby ͷίʔυ ಡΜͰ͍͖͍ͨ
͋Γ͕ͱ͏͍͟͝·ͨ͠