Pro Yearly is on sale from $80 to $50! »

cookpad 2015 summer internship TDD

cookpad 2015 summer internship TDD

17eb0c1a9d70a94ce95401d046375e3c?s=128

Yoshiori SHOJI

August 20, 2015
Tweet

Transcript

  1. TDD ͱ git Ͱ㕩Δ։ൃ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 1

  2. git 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 2

  3. git • βοΫϦݴ͑͹ॻ͍ͨιʔείʔυͷཤྺΛ؅ཧͯ͘͠ΕΔιϑτ • ࠷ॳ͸ΤσΟλͷ undo ͷੌ͍΍ͭͩͱࢥͬͯஔ͚͹ • ศརͳͷͰ༨༟͕Ͱ͖ͨΒ৭ʑௐ΂͓֮ͯ͑ͯ͘ͱྑ͍ •

    github ͱ͔ฉ͍ͨ͜ͱ͋Δͱࢥ͏͚ͲɺͱΓ͋͑ͣؾʹ͢ΔͳɻWiki ͱ Wikipedia ͘Β ͍ͷҧ͍͕͋Δ • ʢ׬શʹؔ܎ͳ͍ͱ͸ݴΘͳ͍͕ಉҰࢹͯ͠͸͍͚ͳ͍ʣ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 3
  4. git ͱΓ֮͋͑ͣ͑Δ΂͖ίϚϯυ - add - มߋΛ൓өͤ͞Δ - commit - ൓өͨ͠มߋΛอଘ͢Δ

    - push - ϦϞʔτʹஔ͘ - pull - ϦϞʔτ͔Β࣋ͬͯ͘Δ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 4
  5. TDD ςετۦಈ։ൃ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 5

  6. TDD ςετۦಈ։ൃ ։ൃख๏ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 6

  7. TDD ςετۦಈ։ൃ ։ൃख๏ → ςετख๏Ͱ͸ͳ͍ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ

    7
  8. TDD • ࠓճ͸ςετϑΝʔετΛ΍ͬͯΈ·͢ • कഁ཭ͷकͷ෦෼ͩͱࢥͬͯ΍ͬͯΈ·͠ΐ͏ • Զ΋݁ߏະͩʹςετϑΝʔετͰॻ͘͜ͱ͕ଟ͍Ͱ͢ 2015/08/20 TDD ͱ

    git Ͱ㕩Δ։ൃ 8
  9. TDD & git ͬͦ͘͞खΛಈ͔͍͖֮ͯ͑ͯ͠·͠ΐ͏ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 9

  10. ՝୊: ԕ͍ੈքͷ਺ࣜ1 ܅ͷ஌Βͳ͍ԕ͍ੈքͷ਺ࣜΛධՁͯ͠΋Β͏ɻ ਺ࣜ͸ 4*5+6&7|8 ͷΑ͏ͳܗʹͳ͍ͬͯΔɻ ۮવʹ΋ɺ ԋࢉࢠͷҙຯ͸ ruby ͷ֤ԋࢉࢠͷҙຯͱಉ͡ͳΜ͚ͩͲɺ༏ઌॱҐ͸

    ruby ͱ͸ҟͳΔͷͰͦͷ·· eval ͢ΔΘ͚ʹ͸͍͔ͳ͍ɻ ԋࢉࢠͷ༏ઌॱҐ͸Լදͷ௨ΓɻҰԠҙຯ΋ॻ͍͚ͨͲɺҙຯ͸ ruby ͱಉ͡ɻ ↑ ༏ઌॱҐߴ | Ϗοτ or & Ϗοτ and + Ճࢉ * ৐ࢉ ↓ ༏ઌॱҐ௿ 1 http://nabetani.sakura.ne.jp/kanagawa.rb/evalex/ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 10
  11. ·ͣ͸؀ڥͷ֬ೝ calculator_spec.rb describe "calculator" do it { expect(true).to be true

    } end ࣮ߦͯ͠ΈΑ͏ $ rspec calculator_spec.rb calculator should equal true Finished in 0.00191 seconds (files took 0.19241 seconds to load) 1 example, 0 failures 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 11
  12. ·ͣ͸؀ڥͷ֬ೝ ͜ΕʹΑͬͯ෼͔Δ͜ͱ 1. rspec ίϚϯυʹ path ͕௨͍ͬͯΔ 1. ·ͣ͸؀ڥͷςετʹͳΔ 2.

    ࣮͸؀ڥ͕੔͍ͬͯͳ͍ͱ͔Α͋͘Δ 2. ࣮ࡍʹ࣮ߦͯ͠ΤϥʔʹͳΒͳ͍ 1. ϕʔεͷίʔυ͕ؒҧ͍͑ͯͳ͍ࣄ͕Θ͔Δ 2. ࣮͸ผͷϑΝΠϧ࣮ߦͯͨ͠ͱ͔Α͋͘Δ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 12
  13. ·ͣ͸؀ڥͷ֬ೝ ͜͜ͰҰ୴ίϛοτ $ git init Initialized empty git repository in

    /path/to/your/dir/.git/ $ git add calculator_spec.rb $ git commit -m "init" [master (root-commit) 1f3ad9b] init 1 file changed, 3 insertions(+) create mode 100644 calculator_spec.rb 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 13
  14. ͔͜͜Β͖͞͸جຊతʹ • ςετ͕ green • ࣮૷ίʔυͷϦϑΝΫλϦϯά͔ςετίʔυͷ ௥Ճ ͷ Έ •

    ςετ͕ red • ࣮૷ίʔυ͔͠࿔Βͳ͍ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 14
  15. ͔͜͜Β͖͞͸جຊతʹ • άνϟάνϟʹͳͬͯΑ͘෼͔Βͳ͘ͳͬͨΒ • git reset --hard HEAD • Ͱɺίϛοτͨ͠ͱ͜Ζ·Ͱ໭ͤ·͢

    • ࠓճ͸ Path ͱ͔Ͱ೰Ήͷຊ࣭Ͱ͸ͳ͍ͷͰҰͭͷϑΝΠϧͰ ΍Γ·͢ • ී௨͸ςετίʔυͱ࣮૷ίʔυͷϑΝΠϧΛΘ͚·͢ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 15
  16. ࠷ॳͷ࣮૷ ·ͣ͸ςετΛίέͤ͞Δ describe "calculator" do it "15*5 = 75" do

    expect(calc("15*5")).to eq 75 end end ࣮ߦͯ͠ΈΔͱίέ·͢ɻ ʮcalc ͳΜͯϝιουແ͍ΑʯͱౖΒΕΔͷͰ calc ͱ͍͏ϝιουΛ࣮ ૷ͯ͠Έ·͢ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 16
  17. ࠷ॳͷ࣮૷ red ʹͳͬͨͷͰ࣮૷ίʔυΛ࿔Δ ςετ͕௨ΔΑ͏ʹ def calc(expression) 75 end describe "calculator"

    do it "15*5 = 75" do expect(calc("15*5")).to eq 75 end end 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 17
  18. !? def calc(expression) 75 end 75 Λฦ͍ͯ͠ΔͷഅࣛΈ͍ͨʁ 2015/08/20 TDD ͱ

    git Ͱ㕩Δ։ൃ 18
  19. ࠷ॳͷ࣮૷ ͜ΕʹΑͬͯ෼͔Δ͜ͱ Ծ࣮૷ ͱ͍ΘΕͨΓ͢Δ 1. green ʹͳΔ͜ͱΛ֬ೝ͢Δ • ظ଴͍ͯ͠Δͱ͓Γʹςετ͕ಈ͍͍ͯΔ͜ͱͷ֬ೝ •

    ςετίʔυͷςετʹͳΔ 2. ࣗ෼͕ฤू͍ͯ͠ΔϑΝΠϧʹؒҧ͍ͳ͍ • ࣗ෼͕৮ͬͯΔϑΝΠϧΛצҧ͍ͯ͠Δͱ͔Α͋͘Δ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 19
  20. ࠷ॳͷ࣮૷ ͜͜ͰҰ୴ green ʹͳͬͨͷͰίϛοτ͓͖ͯ͠·͠ΐ͏ • ࠓ͔ΒϦϑΝΫλϦϯάʹೖΔͷͰ͍ͭͰ΋໭ΕΔΑ͏ʹ • ͋ͱͰ্ॻ͖ͯ͠ཤྺ͔Β͸ຣফ͠·͢ɻ • ޙͰ

    rebase ౳Ͱ·ͱΊΔલఏͰࡉ͔͘ίϛοτͪ͠Ό͏ ͷ΋Φεεϝ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 20
  21. Q.1 ϦϑΝΫλϦϯά Ծ࣮૷Ͱ͸ͳ࣮͘ࡍͷ࣮૷Λͯ͠Έ·͠ΐ͏ʢ5 ෼ʣ def calc(expression) # ίίʹ࣮૷Λॻ͘ end describe

    "calculator" do it "15*5 = 75" do expect(calc("15*5")).to eq 75 end end 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 21
  22. ࠷ॳͷ࣮૷ ྫ def calc(expression) result = nil expression.scan(/\d+/).each do|num| if

    result result = result * num.to_i else result = num.to_i end end result end • green ΛΩʔϓ͍ͯ͠Δ͜ͱ • ઈରʹςετίʔυΛ࿔Βͳ͍ • ඞཁҎ্ͷࣄ͸͠ͳ͍ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 22
  23. ࠷ॳͷ࣮૷ ͜͜Ͱ ίϛοτ $ git add -p ϦϑΝΫλϦϯάͨ͠෼͚ͩͳͷΛ֬ೝ $ git

    commit --amend Ծ࣮૷ͷίϛοτʹ্ॻ͖ͯ͠ίϛοτ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 23
  24. ࣍ͷ࣮૷ ςετΛ·ͣ௥Ճ͠·͢ describe "calculator" do it "15*5 = 75" do

    expect(calc("15*5")).to eq 75 end it "15+5 = 20" do expect(calc("15+5")).to eq 20 end end 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 24
  25. ࣍ͷ࣮૷ 1) calculator 15+5 = 20 Failure/Error: expect(calc("15+5")).to eq 20

    expected: 20 got: 75 (compared using ==) # ./calculator_spec.rb:11:in `block (2 levels) in <top (required)>' 15 + 5 Ͱ 20 Λظ଴͚ͨ͠Ͳ 75 ͕ฦ͖ͬͯͨͱݴ͍ͬͯ·͢ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 25
  26. ࣍ͷ࣮૷ ͜ΕʹΑͬͯ෼͔Δ͜ͱ 1. ৽͍͠མͪΔςετΛ௥Ճ - ςετίʔυͷςετ - ͜Ε͕མͪͳ͔ͬͨΒΦΧγΠ 2. ݩͷςετ͸੒ޭ͍ͯ͠Δ͜ͱΛ֬ೝ

    - ଞΛյ͍ͯ͠ͳ͍͜ͱΛอূ 15 + 5 Ͱ 20 Λظ଴͚ͨ͠Ͳ 75 ͕ฦ͖ͬͯͨͱݴΘΕͨͷͰ 20 ͕ฦΔ Α͏ʹ͠·͢ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 26
  27. ࣍ͷ࣮૷ ·ͨԾ࣮૷Λ͠·͢ def calc(expression) result = nil expression.scan(/\d+/).each do|num| if

    result if expression.include?("*") result = result * num.to_i else result = result + num.to_i end else result = num.to_i end end result end ࠓ౓͸ςετ௨Γ·͢ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 27
  28. ࣍ͷ࣮૷ ͜ΕʹΑͬͯ෼͔Δ͜ͱ 1. ςετ͸ظ଴௨Γʹಈ͍͍ͯΔ • ςετίʔυͷςετ ͳͷͰ͜͜Ͱίϛοτͯ͠ϦϑΝΫλϦϯάʹೖΓ·͢ 2015/08/20 TDD ͱ

    git Ͱ㕩Δ։ൃ 28
  29. ࣍ͷ࣮૷ ϦϑΝΫλϦϯάྫ def calc(expression) result = nil op = expression.scan(/\*|\+/).first

    expression.scan(/\d+/).each do|num| if result case op when "*" result = result * num.to_i else result = result + num.to_i end else result = num.to_i end end result end 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 29
  30. ୤ઢ: Ruby ͷ࿩ "15+5".scan(/\d+/) #=> ["15", "5"] "15+5".send("scan", /\d+/) #=>

    ["15", "5"] ϝιουݺͼग़͠͸ send ϝιουʹจࣈྻͰϝιου໊Λ౉ ͢͜ͱͰߦ͏͜ͱ΋ग़དྷ·͢ɻ(ϝιουͷҾ਺͸ୈೋҾ਺Ҏ߱ ʹฒ΂·͢ʣ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 30
  31. ୤ઢ: Ruby ͷ࿩ 10 + 5 #=> 15 10.+(5) #=>

    15 ԋࢉࢠ΋࣮͸ϝιουͰ͢ɻʢ਺ࣈͷΦϒδΣΫτʹରͯ͠ + ͬͯϝιουΛݺΜͰ͍·͢ʣ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 31
  32. ୤ઢ: Ruby ͷ࿩ ίϨΛ૊Έ߹Θͤͯߟ͑Δͱ 10 + 5 #=> 15 ͸

    10.send("+",5) #=> 15 ͬͯॻ͚·͢ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 32
  33. ୤ઢ: Ruby ͷ࿩ ͱ͍͏͜ͱ͸ case op when "*" result =

    result * num.to_i else result = result + num.to_i end ͱॻ͍ͨ෦෼͸ result = result.send(op, num.to_i) ͱॻ͚·͢ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 33
  34. ୤ઢ: Ruby ͷ࿩ def calc(expression) result = nil op =

    expression.scan(/\*|\+/).first expression.scan(/\d+/).each do|num| if result result = result.send(op, num.to_i) else result = num.to_i end end result end ςετ͕௨ͬͯΔ͜ͱΛ֬ೝͯ͠ίϛοτ͠·͠ΐ͏ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 34
  35. ୤ઢ: Ruby ͷ࿩ Array#map ͱ͍͏ཁૉͷ਺͚ͩ܁Γฦ͠ϒϩοΫΛ࣮ߦͯ͠ϒ ϩοΫͷ໭Γ஋ΛूΊͨ഑ྻΛ࡞੒ͯ͠ฦ͢ϝιου͕͋Γ· ͢ɻ [1, 2, 3].map

    {|i| i + 5 } #=> [6, 7, 8] ["1", "2", "3"].map {|i| i.to_i } #=> [1, 2, 3] 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 35
  36. ୤ઢ: Ruby ͷ࿩ ͦͯ͠୯७ͳϝιουݺͼग़͠ͷ࣌͸ ["1", "2", "3"].map {|i| i.to_i }

    #=> [1, 2, 3] ["1", "2", "3"].map(&:to_i) #=> [1, 2, 3] ͱॻ͚·͢3ɻ 3 Կނͦ͏ͳΔͷ͔ڵຯͷ͋Δਓ͸ Symbol#to_proc Λௐ΂ͯΈ·͠ΐ͏ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 36
  37. ୤ઢ: Ruby ͷ࿩ ͱ͍͏͜ͱ͸ expression.scan(/\d+/) ͱॻ͍͍ͯͨ෦෼Λ expression.scan(/\d+/).map(:to_i) ͱ͓ͯ͘͠ͱຖճ to_i Λݺ͹ͳͯ͘Α͘ͳΓ·͢ɻ

    2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 37
  38. ୤ઢ: Ruby ͷ࿩ def calc(expression) result = nil op =

    expression.scan(/\*|\+/).first expression.scan(/\d+/).map(&:to_i).each do|num| if result result = result.send(op, num) else result = num end end result end ςετ͕௨ͬͯΔ͜ͱΛ֬ೝͯ͠ίϛοτ͠·͠ΐ͏ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 38
  39. ୤ઢ: Ruby ͷ࿩ Enumerable#inject ϝιουͱ͍͏΋ͷ͕͋ΓϒϩοΫΛ࢖ͬͯ ܁Γฦ͠ܭࢉΛߦ͏ͷʹ࢖͍·͢ɻϒϩοΫʹॱʹʮཁૉ1ɺཁ ૉ2ʯɺʮϒϩοΫͷલճͷ໭Γ஋ɺཁૉ3ʯɺʮϒϩοΫͷલճ ͷ໭Γ஋ɺཁૉ4ʯɺ...Λ౉͠·͢ɻϝιουͷ໭Γ஋͸ϒϩο Ϋ͕࠷ޙʹฦͨ͠஋ʹͳΓ·͢ɻ (೔ຊޠͷઆ໌ಡΉΑΓαϯϓϧݟͨ΄͏͕Θ͔Γ΍͍͢Ͱ͆͢ʣ

    [1, 2, 3].inject {|sum, num| sum + num } #=> 6 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 39
  40. ୤ઢ: Ruby ͷ࿩ ͞Βʹ͖ͬ͞ग़͖ͯͨ map ͱಉ͘͡ ["1", "2", "3"].map {|i|

    i.to_i } #=> [1, 2, 3] ["1", "2", "3"].map(&:to_i) #=> [1, 2, 3] ↓ ͷΑ͏ʹॻ͚·͢ [1, 2, 3].inject {|sum, num| sum + num } #=> 6 [1, 2, 3].inject(&:+) #=> 6 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 40
  41. ୤ઢ: Ruby ͷ࿩ ϦϑΝΫλϦϯάྫ def calc(expression) op = expression.scan(/\*|\+/).first expression.scan(/\d+/).map(&:to_i).inject(&op.to_sym)

    end ςετ͕௨ͬͯΔ͜ͱΛ֬ೝͯ͠ίϛοτ͠·͢ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 41
  42. ໌നͳ࣮૷ ࠓͷ࣮૷Ͱ "15&5" ͱ "15|5" ʹ΋؆୯ʹରԠͰ͖ͦ͏Ͱ͢ɻ ͜͏͍͏࣌͸Ծ࣮૷Λল͍࣮ͯ૷ͯ͠͠·͍·͠ΐ͏ɻ ΍͸Γ·ͣ͸ςετΛॻ͖·͢ it "15&5

    = 5" do expect(calc("15&5")).to eq 5 end it "15|5 = 15" do expect(calc("15|5")).to eq 15 end 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 42
  43. ໌നͳ࣮૷ ࣮ߦͯ͠ςετ͕ίέΔ͜ͱΛ֬ೝ͠·͠ΐ͏ େࣄ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 43

  44. େࣄ ͜ͷλΠϛϯάͰςετΛ͓ͯ͘͜͠ͱ͸͘͢͝େࣄͰ͢ɻ େମ͜͏͍͏ʮ͓ɺ༨༟ͰͰ͖ͦ͏ʯͬͯ࣌ʹݶͬͯίϐϖϛεͳͲͰςε τ͕ؒҧ͍͑ͯΔ͜ͱͳͲ͕͋Γ·͢ɻ ͦͯ͠ɺେମͦ͏͍͏ͩ͘Βͳ͍ϋϚΓํͰ࣌ؒΛ࢖ͬͯ͠·ͬͨΓ͠· ͢ɻ ࠓͷ͏ͪʹ·ͣ͸ςετίʔυͷਖ਼͠͞Λ֬ೝ͢ΔบΛ͚͓͖ͭͯ·͠ΐ ͏ɻ 2015/08/20 TDD

    ͱ git Ͱ㕩Δ։ൃ 44
  45. ໌നͳ࣮૷ Ͱ͸ࠓճ͸Ծ࣮૷ͳ͠Ͱ࣮૷ͪ͠Ό͍·͢ɻ def calc(expression) op = expression.scan(/\*|\+|\&|\|/).first expression.scan(/\d+/).map(&:to_i).inject(&op.to_sym) end ςετ͕௨Δ͜ͱΛ֬ೝͯ͠ίϛοτ͠·͢ɻ

    2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 45
  46. ߋʹ࣍΁ ࣍ʹԿΛ࣮૷͢Δ͔Λߟ͑·͢ɻ ݱࡏͷ࢒࡞ۀΛߟ͑Δͱ 1. ԋࢉࢠ͕ࠞࡏ͍ͯ͠Δ࣌ͷॲཧ 2. ԋࢉࢠͷ༏ઌॱҐͷॲཧ ͕࢒͍ͬͯ·͢ɻ 2 ͸̍ΛऴΘΒͤͳ͍ͱ࣮૷Ͱ͖ͦ͏ʹͳ͍ͷͰઌʹ

    1 Λ࣮૷͠ ·͢ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 46
  47. ߋʹ࣍΁ ԋࢉࢠ͕ࠞࡏ͍ͯ͠Δ࣌ͷॲཧ Λ࣮૷͢Δ͜ͱʹܾΊͨͷͰɺద੾ ͳςετΛબͼ·͢ɻ 1. ԋࢉࢠ͕ෳ਺ࠞࡏ͍ͯͯ͠ 2. ༏ઌॱҐΛؾʹ͠ͳ͍Ͱྑ͍ʢࠨ͔Βܭࢉ͢Ε͹ྑ͍ʣ ΋ͷΛ୳͠·͢ɻ αϯϓϧσʔλ1ͷ

    #9 "30+15*5" ͕ྑͦ͞͏Ͱ͢ɻ 1 http://nabetani.sakura.ne.jp/kanagawa.rb/evalex/ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 47
  48. ߋʹ࣍΁ ςετΛ௥Ճ͠མͪΔ͜ͱΛ֬ೝ͠·͢ it "30+15*5 = 225" do expect(calc("30+15*5")).to eq 225

    end 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 48
  49. ߋʹ࣍΁ Ծ࣮૷͠·͢ def calc(expression) return 225 if expression == "30+15*5"

    op = expression.scan(/\*|\+|\&|\|/).first expression.scan(/\d+/).map(&:to_i).inject(&op.to_sym) end ςετ͕௨Δ͜ͱΛ֬ೝ͠·͢ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 49
  50. ߋʹ࣍΁ def calc(expression) op = expression.scan(/\*|\+|\&|\|/) expression.scan(/\d+/).map(&:to_i).inject do|sum, n| sum.send(op.shift,

    n) end end ςετ͕௨ͬͨΒίϛοτ͠·͢ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 50
  51. ׬੒΁ Ͱ͸ɺ࠷ޙʹ࢒ͬͨʮԋࢉࢠͷ༏ઌॱҐͷॲཧʯΛ࣮૷ͯ͠Έ ·͠ΐ͏ɻ αϯϓϧσʔλ1ͷ #6 "30*15+5" ͕ྑͦ͞͏Ͱ͢ɻ ࠓ·Ͱͱಉ͡Α͏ʹςετΛ௥Ճ͠མͪΔ͜ͱΛ֬ೝ͠·͢ɻ it "30*15+5

    = 600" do expect(calc("30*15+5")).to eq 600 end 1 http://nabetani.sakura.ne.jp/kanagawa.rb/evalex/ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 51
  52. ׬੒΁ ࣍ͷ࣮૷Λߟ͑ͯΈ·͠ΐ͏…… it "30*15+5 = 600" do expect(calc("30*15+5")).to eq 600

    end ͪΐͬͱ 5 ෼΄ͲɺίϨ͕௨ΔΑ͏ʹߟ͑ͯΈ͍ͯͩ͘͞ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 52
  53. ׬੒΁ Ͳ͏΍Βࠓ·ͰͷΑ͏ʹ୯७ʹϧʔϓॲཧʹ೚ͤͯܭࢉ͢ΔͷͰ͸͏·͘ߦ͔ ͳͦ͏Ͱ͢ɻ Ұ୴΋͏ͪΐͬͱ؆୯ͳςετ͔Β࢝Ίͨ΄͏͕ྑͦ͞͏Ͱ͢ɻ ͜ͷςετ͸ pending ʹ͠·͢ɻ it "30*15+5 =

    600" do pending("unimplemented") expect(calc("30*15+5")).to eq 600 end ࣮ߦͯ͠ pending ʹͳ͍ͬͯΔ͜ͱΛ֬ೝ͠·͢ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 53
  54. ׬੒΁ ۩ମతʹ͸Ͳ͏΍ͬͯܭࢉ͍ͯ͘͠ͷ͔ߟ͑ͯΈ·͢ɻ 30*15+5 -> 30*20 -> 600 ϓϩάϥϜͰѻ͏ࣄΛߟ͑ͯ Array ʹͯ͠

    ["30", "*", "15", "+", "5"] ["30", "*", "20"] ["600"] ͱܭࢉ͍ͯ͘͠Α͏ʹ͢Ε͹ྑͦ͞͏Ͱ͢ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 54
  55. ׬੒΁ ͱΓ͋͑ͣ࠷ॳͷҰาΛผͷϝιουͰ࣮૷ͯ͠Έ·͠ΐ͏ɻ it "30*15+5 -> 30*20" do expect(_calc(["30", "*", "15",

    "+", "5"])).to eq ["30", "*", "20"] end ্هΛ௨ΔΑ͏ͳίʔυΛ·ͣ͸ॻ͍ͯΈ·͠ΐ͏ɻ ʢ5෼ ׬੒͠ͳͯ͘΋͍͍Ͱ͢ʣ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 55
  56. ׬੒΁ టष͘ॻ͍ͯΈ·ͨ͠ def _calc(expressions) pos = expressions.index("+") operator = expressions[pos]

    left = expressions[pos-1].to_i right = expressions[pos+1].to_i res = left.send(operator, right) expressions[pos-1..pos+1] = res.to_s expressions end ͜ͷॲཧΛԋࢉࢠͷ༏ઌ౓ॱʹߦ͑͹ग़དྷͦ͏Ͱ͢ɻ ͱΓ͋͑ͣίϛοτ͓͖ͯ͠·͢ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 56
  57. ׬੒΁ Q.2 ઌ΄Ͳͷ pending ʹͨ͠ςετ͕௨Δ4Α͏ʹ ࣮૷Λͯ͠Έ·͠ΐ͏ʢ45 ෼ʣ it "30*15+5 =

    600" do pending("unimplemented") expect(calc("30*15+5")).to eq 600 end ͍͖ͳΓ্هςετ௨ΔΑ͏ʹ͢ΔͷͰ͸ͳ͘ ͖ͬ͞ͷΑ͏ʹগͮͭ͠ςετΛॻ͍ͯҰาͮͭਐΜͰΈ·͠ΐ͏ɻ 4 ϖϯσΟϯάʹ͍ͯͨ͠ςετ͕௨Δͱ΋͏ϖϯσΟϯάͰ͸ͳ͍ͱදͨ͢Ίʹ੺͘ͳΔͷͰ pending Λফ͠·͠ΐ͏ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 57
  58. ͕࣍શͯ௨ΔࣄΛ֬ೝͯ͠Έ·͠ΐ͏2 describe "all" do it do [ [ 0, "4*5+6&7|8",

    "44" ], [ 1, "15*5", "75" ], [ 2, "15+5", "20" ], [ 3, "15&5", "5" ], [ 4, "15|5", "15" ], [ 5, "30*15*5", "2250" ], [ 6, "30*15+5", "600" ], [ 7, "30*15&5", "150" ], [ 8, "30*15|5", "450" ], [ 9, "30+15*5", "225" ], [ 10, "30+15+5", "50" ], [ 11, "30+15&5", "35" ], [ 12, "30+15|5", "45" ], [ 13, "30&15*5", "70" ], [ 14, "30&15+5", "19" ], [ 15, "30&15&5", "4" ], [ 16, "30&15|5", "14" ], [ 17, "30|15*5", "155" ], [ 18, "30|15+5", "36" ], [ 19, "30|15&5", "5" ], [ 20, "30|15|5", "31" ], [ 21, "1+2+3+4+5+6+7+8+9+10", "55" ], [ 22, "1*2*3*4*5*6*7*8*9*10", "3628800" ], [ 23, "1+2+3+4+5*6+7+8+9+10", "600" ], [ 24, "1*2*3*4*5+6*7*8*9*10", "1330560" ], [ 25, "1|2|4|8|16|32|64|128|256|512", "1023" ], [ 26, "2046&2045&2043&2039&2031&2015&1983&1919&1791&1535", "1024" ], [ 27, "0+1|7*6", "42" ], [ 28, "6|4+2&9+4", "10" ], [ 29, "0&6+1&6|4*2", "0" ], [ 30, "4|4*7+7+4&9", "56" ], [ 31, "9&8*2+3*1|2|7", "280" ], [ 32, "230+83*751&176", "50080" ], [ 33, "89+62465*94&84", "5254536" ], [ 34, "668&925+398*562", "599092" ], [ 35, "15|9+348*302&23&77", "1452" ], [ 36, "3&3&6|7+3|5*3|2&4*4", "0" ], [ 37, "7*6|7|7*1&7|7&3&8*3", "0" ], [ 38, "896+316*209*264&728", "2026464" ], [ 39, "1844+41*64|7906|66842", "138965970" ], [ 40, "2&41&6884*69857+68083", "0" ], [ 41, "2+3*9*3|6|7&0+3+3*6&8", "0" ], [ 42, "895+400*988|549&237+488", "938875" ], [ 43, "30*48&99036+140&33+75|645", "22050" ], [ 44, "278+2033+53*96*56|3303|3&14", "3177216" ], [ 45, "5380&27|643*2+1888&74+30|16", "0" ], [ 46, "0&3845+6645*4293+78&78*3102|9|3", "90127550385" ], [ 47, "78&44956&67*974|413+13237*5588|54*668", "0" ], [ 48, "500*206+145|167|163|465&668+662+806*681&458", "123896000" ], [ 49, "82+14&0*344+34+542916&18*11|844|64*873223|840993", "23813260003764" ], [ 50, "374958|6727+53965&53*954&29|6*138572+59|547783&43*8998", "12178274756590800" ], ].each do |q| expect(calc(q[1])).to eq q[2].to_i end end 2 https://gist.github.com/yoshiori/87238f1e229baab94a7b 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 58
  59. ྫ def calc(expression) result = expression.scan(/\*|\+|\&|\||\d+/) %w(| & + *).each

    do |op| while result.include? op pos = result.index(op) res = result[pos-1].to_i.send(op, result[pos+1].to_i) result[pos-1..pos+1] = res end end result.first end 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 59
  60. ࠷ॳʹग़͞Ε͓ͨ୊ ܅ͷ஌Βͳ͍ԕ͍ੈքͷ਺ࣜΛධՁͯ͠΋Β͏ɻ ਺ࣜ͸ 4*5+6&7|8 ͷΑ͏ͳܗʹͳ͍ͬͯΔɻ ۮવʹ΋ɺԋࢉࢠͷҙຯ ͸ ruby ͷ֤ԋࢉࢠͷҙຯͱಉ͡ͳΜ͚ͩͲɺ༏ઌॱҐ͸ ruby

    ͱ͸ҟͳΔͷͰͦͷ·· eval ͢ΔΘ͚ʹ͸͍͔ͳ͍ɻ ԋࢉࢠͷ ༏ઌॱҐ͸Լදͷ௨ΓɻҰԠҙຯ΋ॻ͍͚ͨͲɺҙຯ͸ ruby ͱ ಉ͡ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 60
  61. ࣮ࡍʹ๻Β͕࠷ॳʹ΍ͬͨࣄ 15*5 Λ 75 ʹ͢Δ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ

    61
  62. ࠓճͷ՝୊ 1. େ͖͍໰୊Λখ͘͞෼ׂ͢Δ 2. ໰୊Λ 1 ͭͣͭղ͍͍ͯ͘ 3. ͦΕΛTDD Ͱ㕩͍ͯ͘͠

    Λߦ͍·ͨ͠ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 62
  63. TDD ͓͞Β͍ • ςετ͕૝ఆ௨Γ͔֬ೝ͢Δ͜ͱ͕େࣄ • red ͕ѱ͍ɺgreen ͕ྑ͍ͷͰ͸ͳ͍ • red

    ʹͳΔ΂͖ςετ͕ red ʹͳΓɺgreen ʹͳΔ΂͖ςετ͕ green ʹͳ͍ͬͯΔ͜ͱ͕େࣄ • ͦΕΛૉૣ֬͘ೝ͢ΔͨΊͷԾ࣮૷ • ςετίʔυͷςετʹͳΔ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 63
  64. TDD ͓͞Β͍ 1. ࠷ॳʹ red ʹ͢Δ 2. ࠷଎Ͱ green ʹ͢Δ

    • ͜͜·Ͱ͸ςετίʔυͷςετ 3. ϦϑΝΫλϦϯά͢Δ • ͕͜͜ຊؾͷ࣮૷ • ଟ෼ΈΜͳ͕ීஈ૝૾͢Δʢେن໛ͳʣϦϑΝΫλϦϯάͱ͸গ͠ײ͕֮ҧ͏ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 64
  65. red green refactor 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 65

  66. ·ͱΊ 1. େ͖͍໰୊Λখ͘͞෼ׂ͢Δ 2. ໰୊Λ 1 ͭͣͭղ͍͍ͯ͘ 3. ͦΕΛ㕩͍ͯ͘͠ 1.

    ·ͣ͸མͪΔςετΛॻ͘ 2. ςετ͕௨ΔҰ൪؆୯ͳ࣮૷Λॻ͘ʢԾ࣮૷ɺ໌നͳ࣮૷ʣ 3. ϦϑΝΫλϦϯά͢Δ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 66