Upgrade to Pro — share decks privately, control downloads, hide ads and more …

YARV INTRODUCTION

YARV INTRODUCTION

yochiyochirb 100th meetup (2015-12-20)

E3d8c68398580685ee7ef4ec2cc1cfc2?s=128

Hirofumi Wakasugi

December 20, 2015
Tweet

Transcript

  1. YARV INTRODUCTION WAKASUGI HIROFUMI DECEMBER 20, 2015

  2. 3VCZϓϩάϥϜ͕ಈ͘࢓૊ΈͷதͰɺ த৺తͳ໾ׂͷͭΛ઎ΊΔ:"37Λ ؆୯ʹ঺հ͠·͢ɻ YARVINTRODUCTION

  3. :"37 :FU"OPUIFS3VCZ7. w ݱࡏͷ.3* $3VCZ ͷॲཧܥ w 3VCZίʔυΛ࣮ߦ͢ΔͨΊͷԾ૝Ϛγϯ 7. 

    w 3VCZͰ.3*ʹऔΓࠐ·Εͨ w ։ൃऀ͸࡫ాߞҰࢯ YARVINTRODUCTION
  4. Ծ૝Ϛγϯͱ͸ʁ YARVINTRODUCTION

  5. ιϑτ΢ΣΞ͕ಈ࡞͢Δߏ଄ ϋʔυ΢ΣΞ 04 ιϑτ΢ΣΞ ίϯϐϡʔλʔͰ͸ɺ ιϑτ΢ΣΞ͸ࠨهͷ Α͏ͳϨΠϠʔߏ଄Ͱ ಈ͘࢓૊Έͱͳ͍ͬͯ Δ YARVINTRODUCTION

  6. ιϑτ΢ΣΞ͕ಈ࡞͢Δߏ଄ ϋʔυ΢ΣΞ 04 ιϑτ΢ΣΞ ιϑτ΢ΣΞ͸04ͷ"1*Λར༻ 04ͷΧʔωϧ͕ϋʔυ΢ΣΞΛ੍ޚ ιϑτ΢ΣΞ͸04ʹΑͬͯʮԾ૝Խʯ͞Εͨϋʔυ΢ΣΞࢿݯΛ׆༻͢Δɻ ͜ͷΑ͏ʹɺؒʹԾ૝ԽػߏΛڬΉ͜ͱͰΪϟοϓΛຒΊѻ͍΍͘͢ͳΔɻ YARVINTRODUCTION

  7. ҰൠతʹʮԾ૝Ϛγϯʯͱฉ͍ͯࢥ͍ු͔Ϳ΋ͷ ϋʔυ΢ΣΞ 04 ˞ 04 ͋Δ04ͷ্ʹԾ૝Խ͞ΕͨϚγϯΛߏங ͠ɺผͷ04ͳͲΛಈ͔ͤΔ΋ͷ ͨͩɺ:"37ͷΑ͏ͳɺϓϩάϥϛϯάݴ ޠʹ͓͚ΔԾ૝ϚγϯΛߟ͑Δͱ͖ʹ͸ɺ ͜ͷԾ૝ϚγϯͷΠϝʔδʹଊΘΕ͗͢

    ͳ͍ํ͕͍͍͔΋ ˞࣮ࡍʹ͸΄ͱΜͲΦϦδφϧͷ04͸εϧʔͯ͠௚઀ϋʔυ ΢ΣΞΛૢ࡞Ͱ͖Δ͜ͱ͕ଟ͍ Ծ૝Ϛγϯ 7JSUVBM#PYͳͲ ιϑτ΢ΣΞ YARVINTRODUCTION
  8. ϓϩάϥϛϯάݴޠॲཧܥͱͯ͠ͷ ʮԾ૝Ϛγϯʯ ϋʔυ΢ΣΞ 04 ϓϩάϥϛϯάݴޠ ϓϩάϥϛϯάݴޠ͸Ծ૝Ϛγϯ ͕ղऍͰ͖Δ໋ྩʹม׵͞ΕɺԾ ૝Ϛγϯ͕ͦͷ໋ྩΛ04ͳͲΑΓ ௿͍૚ʹ఻͑Δ ͖ͬ͞ͷιϑτ΢ΣΞ͕ಈ࡞͢Δ

    ͨΊͷԾ૝Խػߏͱߟ͑ํ͸ಉ͡ Ծ૝Ϛγϯ YARVINTRODUCTION
  9. 7. 7. 7. ϓϩάϥϛϯάݴޠॲཧܥͱͯ͠ͷ ʮԾ૝Ϛγϯʯ 04 ϓϩάϥϛϯάݴޠ 7. YARVINTRODUCTION 04

    04 04 )8 )8 )8 )8 ͜ͷΑ͏ʹɺϓϩάϥϛϯάݴޠࣗମΛ Ϛϧν؀ڥʹରԠͰ͖ΔΑ͏ઃܭ͢Δͷ ͸ඇৗʹ೉͍͕͠ɺԾ૝ϚγϯΛؒʹڬ Ή͜ͱͰରԠ΍࠷దԽ͕༰қʹͳΔ ݴޠ͸Ծ૝Ϛγϯ͕ղऍͰ͖Δ໋ྩΛు͚Ε͹0, Ծ૝ϚγϯͷϚϧν؀ڥରԠ͸ൺֱత༰қ
  10. Ծ૝Ϛγϯ͕ղऍ͢Δ΋ͷ όΠτίʔυ YARVINTRODUCTION ϓϩάϥϛϯάݴޠ όΠτίʔυ Ծ૝Ϛγϯ͕ղऍ Ͱ͖Δ໋ྩྻ ίϯύΠϧ Ծ૝Ϛγϯ ໋ྩ

    ϓϩάϥϜ࣮ߦʜ
  11. ୅දతͳԾ૝Ϛγϯ YARVINTRODUCTION +BWB+7. w ͓ͳ͡Έʮ8SJUF0ODF 3VO"OZXIFSFʯ w +BWB͸΋ͪΖΜɺ4DBMBɺ$MPKVSFͳͲଟ਺͕͜ͷॲཧܥͰಈ࡞͢Δ /&5'SBNFXPSL w

    .4੡͕ͩ044ͱͯ͠ެ։͞Ε͓ͯΓɺຊདྷϓϥοτϑΥʔϜ͸໰Θͳ͍ w 7#/&5ɺ$ɺ'ͳͲͷݴޠ͕͜ͷॲཧܥͰಈ࡞͢Δ
  12. ୅දతͳԾ૝Ϛγϯ YARVINTRODUCTION +BWB+7. w ͓ͳ͡Έʮ8SJUF0ODF 3VO"OZXIFSFʯ w +BWB͸΋ͪΖΜɺ4DBMBɺ$MPKVSFͳͲଟ਺͕͜ͷॲཧܥͰಈ࡞͢Δ /&5'SBNFXPSL w

    .4੡͕ͩ044ͱͯ͠ެ։͞Ε͓ͯΓɺຊདྷϓϥοτϑΥʔϜ͸໰Θͳ͍ w 7#/&5ɺ$ɺ'ͳͲͷݴޠ͕͜ͷॲཧܥͰಈ࡞͢Δ --7.΋--7.*3ͱ͍͏தؒදݱΛར༻͢ ΔͳͲԾ૝Ϛγϯʹ͍ۙͱ͜Ζ͸͋Δ͕ɺ ݫີʹ͸ίϯύΠϥج൫ͳͷͰ ͪΐͬͱδϟϯϧ͕ҧ͏
  13. Ծ૝ϚγϯͷϝϦοτ·ͱΊ YARVINTRODUCTION w Ϛϧν؀ڥ΁ͷରԠ͕༰қʹͳΔ w ֤؀ڥ΁ͷ࠷దԽ͕͠΍͘͢ߴ଎Խ͕ظ଴Ͱ͖Δ w ผͷϓϩάϥϛϯάݴޠͷ։ൃ΋͠΍͍͢ Ծ૝Ϛγϯͱ͍͏஥հ໾Λ༻ҙ͠ɺϓϩάϥϛϯάݴޠΛ Ծ૝Ϛγϯ΁ͷ໋ྩྻʹίϯύΠϧ͢Δ࢓૊ΈʹΑͬͯɺ

  14. YARVINTRODUCTION YARV

  15. 0WFSWJFX YARVINTRODUCTION 04 3VCZίʔυ :"37 04 04 04 )8 )8

    )8 )8 :"37 :"37 :"37 $ $ $ $ ᶃ3VCZίʔυ͸:"37໋ྩྻͷόΠτίʔυʹί ϯύΠϧ͞ΕΔ ᶄ֤؀ڥʹରԠͨ͠:"37͕ɺڞ௨ͷ:"37໋ྩ ྻΛղऍ͠$ϓϩάϥϜΛੜ੒࣮ͯ͠ߦ w.3*͸$ݴޠ࣮૷ͳͷͰɺ:"37Λհͯ͠$ͷ ϓϩάϥϜͱ࣮ͯ͠ߦ͞ΕΔ wωΠςΟϒίʔυʹม׵͞ΕΔ+7.ͳͲͱ͸ ͜ͷ఺ͰҟͳΔ͕ɺԾ૝Խͷߏ଄తʹ͸ಉ͡
  16. :"37໋ྩྻͱ͸ʁ YARVINTRODUCTION

  17. def hello(name) "Hello " + name end puts hello("Tom") YARVINTRODUCTION

    ͜ͷ3VCZίʔυΛίϯύΠϧ͢Δͱʜʁ
  18. YARVINTRODUCTION == disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>=============== 0000 trace 1 ( 1) 0002

    putspecialobject 1 0004 putspecialobject 2 0006 putobject :hello 0008 putiseq hello 0010 opt_send_without_block <callinfo!mid:core#define_method, argc:3, ARGS_SIMPLE> 0012 pop 0013 trace 1 ( 5) 0015 putself 0016 putself 0017 putstring "Tom" 0019 opt_send_without_block <callinfo!mid:hello, argc:1, FCALL|ARGS_SIMPLE> 0021 opt_send_without_block <callinfo!mid:puts, argc:1, FCALL|ARGS_SIMPLE> 0023 leave == disasm: <RubyVM::InstructionSequence:hello@<compiled>>=============== local table (size: 2, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1]) [ 2] name<Arg> 0000 trace 8 ( 1) 0002 trace 1 ( 2) 0004 putstring "Hello " 0006 getlocal_OP__WC__0 2 0008 opt_plus <callinfo!mid:+, argc:1, ARGS_SIMPLE> 0010 trace 16 ( 3) 0012 leave ( 2) ͜Μͳ:"37໋ྩྻʹͳΔ
  19. == disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>=============== 0000 trace 1 ( 1) 0002 putspecialobject

    1 0004 putspecialobject 2 0006 putobject :hello 0008 putiseq hello 0010 opt_send_without_block <callinfo!mid:core#define_method, argc:3, ARGS_SIMPLE> 0012 pop 0013 trace 1 ( 5) 0015 putself 0016 putself 0017 putstring "Tom" 0019 opt_send_without_block <callinfo!mid:hello, argc:1, FCALL|ARGS_SIMPLE> 0021 opt_send_without_block <callinfo!mid:puts, argc:1, FCALL|ARGS_SIMPLE> 0023 leave == disasm: <RubyVM::InstructionSequence:hello@<compiled>>=============== local table (size: 2, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1]) [ 2] name<Arg> 0000 trace 8 ( 1) 0002 trace 1 ( 2) 0004 putstring "Hello " 0006 getlocal_OP__WC__0 2 0008 opt_plus <callinfo!mid:+, argc:1, ARGS_SIMPLE> 0010 trace 16 ( 3) 0012 leave ( 2) ʁ YARVINTRODUCTION
  20. YARVINTRODUCTION puts 2 + 3 ࣈ۟ղੳ ߏจղੳ 5IF-POHBOE8JOEJOH3PBEUP:"37*OTUSVDUJPOT 3VCZίʔυ ίϯύΠϧ

    :"37໋ྩྻ
  21. YARVINTRODUCTION ࣈ۟ղੳ 3VCZίʔυΛจࣈͣͭಡΈࠐΈղੳ͢Δ 10.times do |n| puts n end

  22. YARVINTRODUCTION ࣈ۟ղੳ 3VCZίʔυΛจࣈͣͭಡΈࠐΈղੳ͢Δ    U J N F

    T E P c O c 10.times do |n| puts n end
  23. YARVINTRODUCTION ࣈ۟ղੳ 3VCZίʔυΛจࣈͣͭಡΈࠐΈղੳ͢Δ    U J N F

    T E P c O c 10.times do |n| puts n end U*/5&(&3   U*%&/5*'*&3 UJNFT LFZXPSE@EP c U*%&/5*'*&3 O c
  24. YARVINTRODUCTION ࣈ۟ղੳ 3VCZίʔυΛจࣈͣͭಡΈࠐΈղੳ͢Δ    U J N F

    T E P c O c 10.times do |n| puts n end U*/5&(&3   U*%&/5*'*&3 UJNFT LFZXPSE@EP c U*%&/5*'*&3 O c τʔΫϯྻ
  25. YARVINTRODUCTION puts 2 + 3 ࣈ۟ղੳ ߏจղੳ 5IF-POHBOE8JOEJOH3PBEUP:"37*OTUSVDUJPOT 3VCZίʔυ ίϯύΠϧ

    :"37໋ྩྻ ίʔυΛτʔΫϯྻʹ෼ղ
  26. YARVINTRODUCTION ߏจղੳ U*/5&(&3   U*%&/5*'*&3 UJNFT LFZXPSE@EP c U*%&/5*'*&3

    O c method_call: ... | primary_value '.' operation2 | ... จ๏نଇʹϚον ύʔα͕จ๏نଇʹج͍ͮͯτʔΫϯྻͷղੳΛߦ͏
  27. লུʜ YARVINTRODUCTION ߏจղੳ QSPHSBN NFUIPE
 BEECMPDL DBMM JOUFHFS 10 QFSJPE

    JEFOUJpFS times EPCMPDL ߏจղੳʹΑͬͯ"45 ந৅ߏจ໦ ͕ੜ੒͞ΕΔ
  28. YARVINTRODUCTION ߏจղੳ 3JQQFSΛ࢖͏ͱ࣮ࡍʹ"45Λ֬ೝͰ͖Δ require 'ripper' require 'pp' code = <<EOS

    10.times do |n| puts n end EOS pp Ripper.sexp(code) [:program, [[:method_add_block, [:call, [:@int, "10", [1, 0]], :".", [:@ident, "times", [1, 3]]], [:do_block, [:block_var, [:params, [[:@ident, "n", [1, 13]]], nil, nil, nil, nil, nil, nil], false], [[:command, [:@ident, "puts", [2, 2]], [:args_add_block, [[:var_ref, [:@ident, "n", [2, 7]]]], false]]]]]]] 4ࣜʂ
  29. YARVINTRODUCTION puts 2 + 3 ࣈ۟ղੳ ߏจղੳ 5IF-POHBOE8JOEJOH3PBEUP:"37*OTUSVDUJPOT 3VCZίʔυ ίϯύΠϧ

    :"37໋ྩྻ ίʔυΛτʔΫϯྻʹ෼ղ τʔΫϯྻ͔Β"45Λੜ੒
  30. YARVINTRODUCTION :"37໋ྩྻ΁ͷίϯύΠϧ ΑΓ୯७ͳ3VCZίʔυΛݩʹઆ໌ puts 2 + 3 3VCZίʔυ /0%&@4$01& /0%&@'$"--

    ̼FUIPE@JEputs /0%&@$"-- NFUIPE@JE+ /0%&@-*5 2 /0%&@-*5 3 Ϩγʔό Ҿ਺ "45ϊʔυ
  31. putself putobject 2 putobject 3 send <callinfo!mid:+, argc:1, … send

    <callinfo!mid:puts, argc:1, ... YARVINTRODUCTION :"37໋ྩྻ΁ͷίϯύΠϧ "45ߏ଄ΛḷΓͳ͕Β :"37໋ྩྻελοΫʹϓογϡ͍ͯ͘͠ /0%&@'$"-- ̼FUIPE@JEputs /0%&@$"-- NFUIPE@JE+ :"37໋ྩྻ /0%&@-*5 2 /0%&@-*5 3
  32. putself putobject 2 putobject 3 opt_plus opt_send_simple <callinfo!mid:puts, argc:1, ...

    YARVINTRODUCTION :"37໋ྩྻ΁ͷίϯύΠϧ :"37໋ྩྻ͸࠷దԽ͞ΕΔ /0%&@'$"-- ̼FUIPE@JEputs /0%&@$"-- NFUIPE@JE+ :"37໋ྩྻ /0%&@-*5 2 /0%&@-*5 3 ߋʹ࠷దԽΛߦ͍ɺ ࠷దԽ໋ͨ͠ྩʹஔ͖׵͑Δ
  33. YARVINTRODUCTION puts 2 + 3 ࣈ۟ղੳ ߏจղੳ 5IF-POHBOE8JOEJOH3PBEUP:"37*OTUSVDUJPOT 3VCZίʔυ ίϯύΠϧ

    :"37໋ྩྻ ίʔυΛτʔΫϯྻʹ෼ղ τʔΫϯྻ͔Β"45Λੜ੒ "45͔Β࠷దԽ͞Εͨ :"37໋ྩྻ όΠτίʔυ Λੜ੒
  34. YARVINTRODUCTION :"37໋͕ྩʹै͍$ϓϩάϥϜΛ࣮ߦ ͜ͷΑ͏ʹͯ͠3VCZϓϩάϥϜ͸࣮ߦ͞ΕΔ $ cat example.rb puts 2 + 3

    $ ruby example.rb 5 ࣈ۟ղੳɺ ߏจղੳɺ :"37໋ྩྻ΁ͷίϯύΠϧɺ $ϓϩάϥϜͷ࣮ߦ
  35. YARVINTRODUCTION ·ͱΊ 3VCZͷ͘͠ΈΛ஌Δͱ 3VCZ,BJHJΛ΋ͬͱָ͠ΊΔʂ

  36. YARVINTRODUCTION ࢀߟ ˒ :"37.BOJBDT w IUUQNBHB[JOFSVCZJTUOFU :BSW.BOJBDT ˒ 3VCZͷ͘͠Έ3VCZ6OEFSB.JDSPTDPQF w

    IUUQTFTUPSFPINTIBDPKQUJUMFT1 ҎԼͷهࣄ΍ॻ੶Λࢥ͍͖ͬΓࢀߟʹ͠·ͨ͠
  37. よちよち.rb 回オメデトウ