Static typed Ruby starting with YARD

Static typed Ruby starting with YARD

Meguro.rb#16 で「YARD を利用してRubyで静的型付きプログラミングをする話」をしました
cf. https://megurorb.connpass.com/event/90916/

2b4f9f20b554d4c77c46fe6a9930d6fe?s=128

Nao Minami

June 28, 2018
Tweet

Transcript

  1. ©2018 Wantedly, Inc. YARDͰ࢝ΊΔ ੩తܕ෇͖Ruby 28.Jun.2018 - Nao Minami (@south37)

  2. ©2018 Wantedly, Inc. ࣗݾ঺հ

  3. ©2018 Wantedly, Inc. Nao Minami / @south37, @minami7o Infrastructure engineer

    @ Wantedly, Inc.
  4. ©2018 Wantedly, Inc. ಥવͰ͕͢ɻɻɻ

  5. ©2018 Wantedly, Inc.

  6. ©2018 Wantedly, Inc. 3VCZ,JHJ ྑ͔ͬͨͰ͢Ͷʂ

  7. ©2018 Wantedly, Inc. ৭ʑͳτʔΫ͕͋Γ·͕ͨ͠ɺ 4UFFQͱ͍͏ʮ੩తܕ෇͚πʔϧʯ͕ ҹ৅ʹ࢒Γ·ͨ͠

  8. ©2018 Wantedly, Inc.

  9. ©2018 Wantedly, Inc. 4UFFQͱ͸ w 3VCZʹରͯ͠੩తܕ෇͚͕ग़དྷΔπʔϧ w ܕఆٛϑΝΠϧͱ3VCZίʔυ͔ΒɺܕݕࠪΛߦ͏

  10. ©2018 Wantedly, Inc. 4UFFQͷܕఆٛϑΝΠϧ class Person @name: String @contacts: Array<Email

    | Phone> def initialize: (name: String) -> any def name: -> String def contacts: -> Array<Email | Phone> def guess_country: -> (String | nil) end
  11. ©2018 Wantedly, Inc. 4UFFQͷ1SPT$POT w 1SPT w 3VCZͰ੩తܕݕ͕ࠪग़དྷΔʂιϑτ΢ΣΞ։ൃͷॿ͚ʹͳͬͯ͘ΕΔʂ w $POT

    w ܕఆٛϑΝΠϧͷઐ༻จ๏Λ֮͑Δඞཁ͕͋Δ w ܕఆٛϑΝΠϧΛ͔Βॻ͘ͷ͕େมʂ w ATUFFQTDB⒎PMEAίϚϯυ͕ॿ͚ʹͳͬͯ͘ΕΔ͕ɻɻɻ
  12. ©2018 Wantedly, Inc. Կ͔ྑ͍ํ๏͸ແ͍͔ʁ

  13. ©2018 Wantedly, Inc. 3VCZͰܕͱ͍͑͹:"3%ʜ :"3%ͰܕνΣοΫΛߦ͑͹ྑ͍ͷ͸ʁ

  14. ©2018 Wantedly, Inc. ͱ͍͏͜ͱͰɺ πʔϧΛ࡞ͬͯΈͨ

  15. ©2018 Wantedly, Inc.

  16. ©2018 Wantedly, Inc.

  17. ©2018 Wantedly, Inc. ZBSETUFFQ

  18. ©2018 Wantedly, Inc.  എܠ  ZBSETUFFQͷ͝঺հ  ·ͱΊ ࠓ೔࿩͢಺༰

  19. ©2018 Wantedly, Inc. ZBSETUFFQͱ͸ w 3VCZϓϩάϥϜதͷ:"3%Ξϊʔςʔγϣϯ ͔Βɺ4UFFQͷܕఆٛϑΝΠϧΛࣗಈੜ੒ w 4UFFQͱ૊Έ߹ΘͤΔࣄͰɺ:"3%Ͱ੩తܕݕ ࠪΛ࣮ݱʂ

  20. ©2018 Wantedly, Inc. %&.0

  21. ©2018 Wantedly, Inc. 3VCZίʔυΛ༻ҙ͢Δ # lib/parser.rb class AST # @return

    [String] def to_s "AST" end # @param [Hash] opt def traverse!(opt) end end class Parser # @param [String] text # @param [Hash] opt # @return [AST] def parse(text, opt) ast = AST.new ast.traverse!(opt) ast end end
  22. ©2018 Wantedly, Inc. ZBSETUFFQίϚϯυΛ࣮ߦ $ yard2steep lib ZBSETUFFQͰܕఆٛϑΝΠϧΛੜ੒ # sig/parser.rbi

    class AST def to_s: -> String def traverse!: (Hash<any, any>) -> any end class Parser def parse: (String, Hash<any, any>) -> AST end
  23. ©2018 Wantedly, Inc. TUFFQͰܕνΣοΫ $ steep check lib ܕݕ͕ࠪ௨Ε͹ɺԿ΋ग़ྗ͞Εͳ͍

  24. ©2018 Wantedly, Inc. Ͱ͸ɺܕݕ͕ࣦࠪഊ͢Δέʔε͸Ͳ͏ͩΖ͏ʁ

  25. ©2018 Wantedly, Inc. ܕݕࠪͰࣦഊͤͯ͞ΈΔ # lib/parser.rb class AST # @return

    [String] def to_s "AST" end # @param [Hash] opt def traverse!(opt) end end class Parser # @param [String] text - # @param [Hash] opt + # @param [Hash, nil] opt # @return [AST] - def parse(text, opt) + def parse(text, opt: nil) ast = AST.new ast.traverse!(opt) ast end end
  26. ©2018 Wantedly, Inc. ܕݕࠪͰࣦഊͤͯ͞ΈΔ $ yard2steep lib && steep check

    lib lib/parser.rb:18:18: ArgumentTypeMismatch: receiver=::AST, expected=::Hash<any, any>, actual=(::Hash<any, any> | nil) (opt) ZBSETUFFQͱTUFFQΛ૊Έ߹ΘͤͯܕνΣοΫ ͪΌΜͱɺAOJMAνΣοΫ͍ͯ͠ͳ͍ίʔυͰΤϥʔ ͕ग़ͨʂ
  27. ©2018 Wantedly, Inc. :"3%Ͱ੩తܕݕ͕ࠪग़དྷͯΔʂ

  28. ©2018 Wantedly, Inc. ZBSETUFFQͷࠓޙʹ͍ͭͯ w ΍Γ͍ͨࣄ͸ͨ͘͞Μ w :"3%BOOPUBUJPOͷϦονͳه๏Λαϙʔτ w BUUS@

    ͳͲɺ3VCZͷ૊ΈࠐΈͷNFUBQSPHSBNNJOHܥͷػೳΛTVQQPSU w 3VCZQBSTFSͷվળ ࣗલ࣮૷3JQQFSϕʔε΁Ҡߦ  w FUD w 4UFFQଆʹ΋DPOUSJCVUF͍ͨ͠ w CVJMUJONFUIPEͷܕ෇͚ w PQFODMBTTͷTVQQPSU w CVHpY ػೳ௥Ճ
  29. ©2018 Wantedly, Inc.  എܠ  ZBSETUFFQͷ͝঺հ  ·ͱΊ ࠓ೔࿩͢಺༰

  30. ©2018 Wantedly, Inc. ·ͱΊ w :"3%Ͱ੩తܕݕ͕ࠪͰ͖ΔΑ͏ʹͳͬͨʂ w 4UFFQͱZBSETUFFQΛ૊Έ߹ΘͤΔࣄͰ࣮ݱ w BOZܕ͕͋ΔͷͰɺগͣͭ͠ಋೖ͕Մೳ

    w ͜Ε͔Β΋ɺ3VCZͰ)BQQZ)BDLJOH͠Α͏ʂ
  31. ©2018 Wantedly, Inc.  എܠ  ZBSETUFFQͷ͝঺հ  ·ͱΊ 

    ͓·͚ ࠓ೔࿩͢಺༰
  32. ©2018 Wantedly, Inc. ͓·͚େมͩͬͨࣄ w 3VCZίʔυͷQBSTFSΛࣗલ࣮૷ w ίϯςΩετΛ೺Ѳ͢ΔͨΊʹFOEΛೝࣝ͢Δඞཁ͕͋ͬͯɺ͔ͳΓେมͩͬͨɻɻɻޙஔJG Ή͔͍ͣ͠ɻɻɻ w

    3JQQFSTFYQͩͱίϝϯτ͕ফ͑ͯ͠·͏ͷͰࣗલʹ͕ͨ͠ɺ3JQQFSϕʔεʹҠߦ༧ఆʂ w 4UFFQͷڍಈ w ADMBTTTFMGA͕TVQQPSU͞Ε͍ͯͳ͔ͬͨΓɺCVJMUJODMBTT΁ͷܕ෇͚͕଍Γͳͯ͘खؒ औͬͨ w AJGdA΍ANBQ TUSJQ A͕͏·͘ಈ͔ͳ͔ͬͨΓɺAEFGOJM  CPPMA͕QBSTFFSSPS ʹͳͬͯۤ͠ΊΒΕͨ