Sorbetを触ってみた話

Cc713022dd634a5a4985136378eeb429?s=47 ryoh17338
September 25, 2019

 Sorbetを触ってみた話

nikotama.rb #5 (2019.09.25)

Cc713022dd634a5a4985136378eeb429?s=128

ryoh17338

September 25, 2019
Tweet

Transcript

  1. Re:valua t ion https://fril.jp/ L ead & Delight User F

    irst Ownership Fail Smart 4PSCFUΛ৮ͬͯΈͨ࿩
  2. 3BLVNB4FSWJDF%FWFMPQNFOU(SPVQ 3BLVUFO JOD "CPVUNF ௕୩෦྄)BTFCF3ZP !%FBS@ZPV@DSZ ܦྺ ˝/55ܥ4*FSʢۚ༥൚༻ػ೥ʣ ˝ݹΊͷϕϯνϟʔʢ8FCडୗ೥ʣ ˝৽͠Ίͷϕϯνϟʔʢਓࣄ࿑຿ܥ4BB4೥ʣ

    ⾣ָఱʢʙʣ
  3. None
  4. ੵۃ࠾༻தʂ

  5. 4PSCFUͬͯʁ  

  6.   "CPVU4PSCFU • Ruby޲͚ܕνΣοΧʔ • ࡶʹݴ͏ͱɺrubocopͷܕόʔδϣϯ • srb/sorbet-runtimeͰߏ੒͞ΕΔ •

    srb: CLI/੩తνΣοΫ/ηοτΞοϓ • sorbet-runtime: T::Sig(ޙड़)/ಈతνΣοΫ
  7. ܕΛνΣοΫͰ͖Δͱɺͳʹ͕ྑ͍ʁ  

  8.   ͜͜ʹ͸λΠτϧΛೖྗ͠·͢ɻ ܕΛνΣοΫͰ͖Δͱ ιʔείʔυͷϝϯςφϯεੑ͕޲্͢Δ

  9.   ྫ͑͹ class Hoge def add(a,b) a + b

    end end Hoge.new.add(1,2) => 3 # ظ଴௨Γ Hoge.new.add('1','2') => “12" # ͜ͷϨϕϧͳΒྑ͍͚Ͳɺ ෳࡶ౓͕૿ͨ͠ίʔυΛԿਓ΋ͷਓ͕ݟΔͱ ඞͣೝࣝͷᴥᴪ͕ൃੜͯ͠͠·͏
  10. 4PSCFU͸Կ͕Ͱ͖Δ͔  

  11.   ͜͜ʹ͸λΠτϧΛೖྗ͠·͢ɻ ܕʹىҼ͢ΔϛεΛ࣮ߦલʹݕ஌Ͱ͖Δ ɾҾ਺ͷܕ͕ҧ͏ ɾϝιου໊ͷλΠϙ

  12.   ͜͜ʹ͸λΠτϧΛೖྗ͠·͢ɻ class A # ϝιου஫ऍʹ࢖༻͢ΔɺsigϝιουΛ࢖͑ΔΑ͏ʹ͢Δ extend T::Sig #

    sigΛ࢖ͬͯҾ਺ͱฦΓ஋ͷܕΛఆٛ͢Δ sig {params(x: Integer).returns(String)} def bar(x) x.to_s end end def main A.new.barr(91) # error: Typo! A.new.bar("91") # error: Type mismatch! end editor.rb:11: Method barr does not exist on A https://srb.help/7003 11 | A.new.barr(91) # error: Typo! ^^^^^^^^^^^^^^ Autocorrect: Use `-a` to autocorrect editor.rb:11: Replace with bar 11 | A.new.barr(91) # error: Typo! ^^^^ editor.rb:12: Expected Integer but found String("91") for argument x https://srb.help/7002 12 | A.new.bar("91") # error: Type mismatch! ^^^^^^^^^^^^^^^ editor.rb:4: Method A#bar has specified x as Integer 4 | sig {params(x: Integer).returns(String)} ^ Got String("91") originating from: editor.rb:12: 12 | A.new.bar("91") # error: Type mismatch! ^^^^ Errors: 2
  13. ಋೖ  

  14.   ͜͜ʹ͸λΠτϧΛೖྗ͠·͢ɻ # -- Gemfile -- gem 'sorbet', :group

    => :development gem 'sorbet-runtime' › bundle install
  15.   ͜͜ʹ͸λΠτϧΛೖྗ͠·͢ɻ › bundle exec srb init ্هίϚϯυͰ
 ɾsorbetϑΥϧμ͕࡞੒͞ΕΔ

    ɾطଘίʔυͷ্෦ʹ # typed: strict ͱ͔͕ ɹૠೖ͞ΕΔʢܕνΣοΫͷݫີ౓ΛઃఆͰ͖Δʣ
  16.   ͜͜ʹ͸λΠτϧΛೖྗ͠·͢ɻ # typed: strict class UsersController < ApplicationController

    skip_before_action :require_sign_in, only: [:new, :create] ignore: ͢΂ͯແࢹ͢Δʢݕࠪ͠ͳ͍ʣ false: ߏจΤϥʔͱsig sͷਖ਼֬ੑ͚ͩใࠂʢσϑΥϧτʣ true: ଘࡏ͠ͳ͍ϝιου/Ҿ਺Χ΢ϯτ/ม਺ͷܕͷໃ६౳Λใࠂ strict: ͢΂ͯͷϝιουʹsig͕ඞཁ/͢΂ͯͷఆ਺ͱΠϯελϯεఆ਺ʹܕ ࢦఆ͕ඞཁ strong: sorbet͕ϑΝΠϧ಺ͷ͢΂ͯͷϝιουίʔϧʹରͯ͠ܕΛ೺Ѳͯ͠ ͍Δ͜ͱΛٻΊΔʢΊͬͨʹ࢖༻͠ͳ͍ʣ
  17.   ͜͜ʹ͸λΠτϧΛೖྗ͠·͢ɻ sorbet/ ├── config └── rbi/ └── ···

    config: Sorbet͕ಡΈऔΔઃఆϑΝΠϧ rbi: RBIϑΝΠϧʢܕγάωνϟʣΛؚΉϑΥϧμ ɹɹRBIʢRuby InterfaceʣϑΝΠϧ͸ɺSorbet͔Β͸ݟ͑ͳ͍
 ɹɹRubyͷΫϥεɺϞδϡʔϧɺఆ਺ɺ͓ΑͼϝιουΛSorbetʹ
 ɹɹએݴ͢Δ
  18.   ͜͜ʹ͸λΠτϧΛೖྗ͠·͢ɻ › bundle exec srb tc ্هίϚϯυͰ
 ɾશιʔείʔυͷํνΣοΫΛߦ͏

    ɾ͜ͷஈ֊Ͱ͸ɺ͢΂ͯͷΤϥʔΛແࢹ͢Δ ɹɹɾrubocopಋೖ࣌ͷ.rubocop_todo.ymlΈ͍ͨͳײ͡
  19. σϞ  

  20. ·ͱΊ  

  21.   ͜͜ʹ͸λΠτϧΛೖྗ͠·͢ɻ Good - RubyͰܕΛνΣοΫͰ͖ɺϝϯςφϯεੑ͕૿͢
 - ஈ֊తʹಋೖͰ͖ΔʢϑΝΠϧ୯ҐͰݫີੑΛࢦఆʣ Bad
 -

    ίʔυͷݟ௨͕͠ѱ͘ͳΔ - 1.0͕ग़ͯͳ͍ʢ࠷৽͸0.4.4782ʣͷͰϦεΫ༗
  22.   ͜͜ʹ͸λΠτϧΛೖྗ͠·͢ɻ ΋͠ϓϩμΫτίʔυʹSorbetΛಋೖ͢Δͱͨ͠Β… ܾࡁॲཧ෦෼ͷΑ͏ʹɺόάࠞೖͷϦεΫΛԼ͍͛ͨ ՕॴʹϐϯϙΠϯτͰಋೖ͢Δͷ͕ྑͦ͞͏

  23.   5IBOLZPVGPSMJTUFOJOH