Share
Embed
Transcript
66 slides
-
git • βοΫϦݴ͑ॻ͍ͨιʔείʔυͷཤྺΛཧͯ͘͠ΕΔιϑτ • ࠷ॳΤσΟλͷ undo ͷੌ͍ͭͩͱࢥͬͯஔ͚ • ศརͳͷͰ༨༟͕Ͱ͖ͨΒ৭ʑௐ͓֮ͯ͑ͯ͘ͱྑ͍ •
github ͱ͔ฉ͍ͨ͜ͱ͋Δͱࢥ͏͚ͲɺͱΓ͋͑ͣؾʹ͢ΔͳɻWiki ͱ Wikipedia ͘Β ͍ͷҧ͍͕͋Δ • ʢશʹؔͳ͍ͱݴΘͳ͍͕ಉҰࢹ͍͚ͯ͠ͳ͍ʣ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 3 -
git ͱΓ֮͋͑ͣ͑Δ͖ίϚϯυ - add - มߋΛөͤ͞Δ - commit - өͨ͠มߋΛอଘ͢Δ
- push - ϦϞʔτʹஔ͘ - pull - ϦϞʔτ͔Β࣋ͬͯ͘Δ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 4 -
՝: ԕ͍ੈքͷࣜ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 -
·ͣڥͷ֬ೝ 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 -
·ͣڥͷ֬ೝ ͜ΕʹΑ͔ͬͯΔ͜ͱ 1. rspec ίϚϯυʹ path ͕௨͍ͬͯΔ 1. ·ͣڥͷςετʹͳΔ 2.
࣮ڥ͕͍ͬͯͳ͍ͱ͔Α͋͘Δ 2. ࣮ࡍʹ࣮ߦͯ͠ΤϥʔʹͳΒͳ͍ 1. ϕʔεͷίʔυ͕ؒҧ͍͑ͯͳ͍ࣄ͕Θ͔Δ 2. ࣮ผͷϑΝΠϧ࣮ߦͯͨ͠ͱ͔Α͋͘Δ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 12 -
·ͣڥͷ֬ೝ ͜͜ͰҰ୴ίϛοτ $ 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 -
͔͜͜Β͖͞جຊతʹ • ςετ͕ green • ࣮ίʔυͷϦϑΝΫλϦϯά͔ςετίʔυͷ Ճ ͷ Έ •
ςετ͕ red • ࣮ίʔυ͔͠࿔Βͳ͍ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 14 -
͔͜͜Β͖͞جຊతʹ • άνϟάνϟʹͳͬͯΑ͔͘Βͳ͘ͳͬͨΒ • git reset --hard HEAD • Ͱɺίϛοτͨ͠ͱ͜Ζ·Ͱͤ·͢
• ࠓճ Path ͱ͔ͰΉͷຊ࣭Ͱͳ͍ͷͰҰͭͷϑΝΠϧͰ Γ·͢ • ී௨ςετίʔυͱ࣮ίʔυͷϑΝΠϧΛΘ͚·͢ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 15 -
࠷ॳͷ࣮ ·ͣςετΛίέͤ͞Δ 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 -
࠷ॳͷ࣮ 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 -
࠷ॳͷ࣮ ͜ΕʹΑ͔ͬͯΔ͜ͱ Ծ࣮ ͱ͍ΘΕͨΓ͢Δ 1. green ʹͳΔ͜ͱΛ֬ೝ͢Δ • ظ͍ͯ͠Δͱ͓Γʹςετ͕ಈ͍͍ͯΔ͜ͱͷ֬ೝ •
ςετίʔυͷςετʹͳΔ 2. ͕ࣗฤू͍ͯ͠ΔϑΝΠϧʹؒҧ͍ͳ͍ • ͕ࣗ৮ͬͯΔϑΝΠϧΛצҧ͍ͯ͠Δͱ͔Α͋͘Δ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 19 -
࠷ॳͷ࣮ ͜͜ͰҰ୴ green ʹͳͬͨͷͰίϛοτ͓͖ͯ͠·͠ΐ͏ • ࠓ͔ΒϦϑΝΫλϦϯάʹೖΔͷͰ͍ͭͰΕΔΑ͏ʹ • ͋ͱͰ্ॻ͖ͯ͠ཤྺ͔Βຣফ͠·͢ɻ • ޙͰ
rebase Ͱ·ͱΊΔલఏͰࡉ͔͘ίϛοτͪ͠Ό͏ ͷΦεεϝ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 20 -
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 -
࠷ॳͷ࣮ ྫ 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 -
࠷ॳͷ࣮ ͜͜Ͱ ίϛοτ $ git add -p ϦϑΝΫλϦϯά͚ͨͩ͠ͳͷΛ֬ೝ $ git
commit --amend Ծ࣮ͷίϛοτʹ্ॻ͖ͯ͠ίϛοτ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 23 -
࣍ͷ࣮ ςετΛ·ͣՃ͠·͢ 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 -
࣍ͷ࣮ 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 -
࣍ͷ࣮ ͜ΕʹΑ͔ͬͯΔ͜ͱ 1. ৽͍͠མͪΔςετΛՃ - ςετίʔυͷςετ - ͜Ε͕མͪͳ͔ͬͨΒΦΧγΠ 2. ݩͷςετޭ͍ͯ͠Δ͜ͱΛ֬ೝ
- ଞΛյ͍ͯ͠ͳ͍͜ͱΛอূ 15 + 5 Ͱ 20 Λظ͚ͨ͠Ͳ 75 ͕ฦ͖ͬͯͨͱݴΘΕͨͷͰ 20 ͕ฦΔ Α͏ʹ͠·͢ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 26 -
࣍ͷ࣮ ·ͨԾ࣮Λ͠·͢ 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 -
࣍ͷ࣮ ϦϑΝΫλϦϯάྫ 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 -
ઢ: Ruby ͷ "15+5".scan(/\d+/) #=> ["15", "5"] "15+5".send("scan", /\d+/) #=>
["15", "5"] ϝιουݺͼग़͠ send ϝιουʹจࣈྻͰϝιου໊Λ ͢͜ͱͰߦ͏͜ͱग़དྷ·͢ɻ(ϝιουͷҾୈೋҾҎ߱ ʹฒ·͢ʣ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 30 -
ઢ: Ruby ͷ 10 + 5 #=> 15 10.+(5) #=>
15 ԋࢉࢠ࣮ϝιουͰ͢ɻʢࣈͷΦϒδΣΫτʹରͯ͠ + ͬͯϝιουΛݺΜͰ͍·͢ʣ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 31 -
ઢ: Ruby ͷ ίϨΛΈ߹Θͤͯߟ͑Δͱ 10 + 5 #=> 15
10.send("+",5) #=> 15 ͬͯॻ͚·͢ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 32 -
ઢ: 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 -
ઢ: 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 -
ઢ: 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 -
ઢ: 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 -
ઢ: Ruby ͷ ͱ͍͏͜ͱ expression.scan(/\d+/) ͱॻ͍͍ͯͨ෦Λ expression.scan(/\d+/).map(:to_i) ͱ͓ͯ͘͠ͱຖճ to_i Λݺͳͯ͘Α͘ͳΓ·͢ɻ
2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 37 -
ઢ: 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 -
ઢ: Ruby ͷ Enumerable#inject ϝιουͱ͍͏ͷ͕͋ΓϒϩοΫΛͬͯ ܁Γฦ͠ܭࢉΛߦ͏ͷʹ͍·͢ɻϒϩοΫʹॱʹʮཁૉ1ɺཁ ૉ2ʯɺʮϒϩοΫͷલճͷΓɺཁૉ3ʯɺʮϒϩοΫͷલճ ͷΓɺཁૉ4ʯɺ...Λ͠·͢ɻϝιουͷΓϒϩο Ϋ͕࠷ޙʹฦͨ͠ʹͳΓ·͢ɻ (ຊޠͷઆ໌ಡΉΑΓαϯϓϧݟͨ΄͏͕Θ͔Γ͍͢Ͱ͆͢ʣ
[1, 2, 3].inject {|sum, num| sum + num } #=> 6 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 39 -
ઢ: 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 -
ઢ: 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 -
໌നͳ࣮ ࠓͷ࣮Ͱ "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 -
ߋʹ࣍ ࣍ʹԿΛ࣮͢Δ͔Λߟ͑·͢ɻ ݱࡏͷ࡞ۀΛߟ͑Δͱ 1. ԋࢉࢠ͕ࠞࡏ͍ͯ͠Δ࣌ͷॲཧ 2. ԋࢉࢠͷ༏ઌॱҐͷॲཧ ͕͍ͬͯ·͢ɻ 2 ̍ΛऴΘΒͤͳ͍ͱ࣮Ͱ͖ͦ͏ʹͳ͍ͷͰઌʹ
1 Λ࣮͠ ·͢ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 46 -
ߋʹ࣍ ԋࢉࢠ͕ࠞࡏ͍ͯ͠Δ࣌ͷॲཧ Λ࣮͢Δ͜ͱʹܾΊͨͷͰɺద ͳςετΛબͼ·͢ɻ 1. ԋࢉࢠ͕ෳࠞࡏ͍ͯͯ͠ 2. ༏ઌॱҐΛؾʹ͠ͳ͍Ͱྑ͍ʢࠨ͔Βܭࢉ͢Εྑ͍ʣ ͷΛ୳͠·͢ɻ αϯϓϧσʔλ1ͷ
#9 "30+15*5" ͕ྑͦ͞͏Ͱ͢ɻ 1 http://nabetani.sakura.ne.jp/kanagawa.rb/evalex/ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 47 -
ߋʹ࣍ ςετΛՃ͠མͪΔ͜ͱΛ֬ೝ͠·͢ it "30+15*5 = 225" do expect(calc("30+15*5")).to eq 225
end 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 48 -
ߋʹ࣍ Ծ࣮͠·͢ 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 -
ߋʹ࣍ 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 -
Ͱɺ࠷ޙʹͬͨʮԋࢉࢠͷ༏ઌॱҐͷॲཧʯΛ࣮ͯ͠Έ ·͠ΐ͏ɻ αϯϓϧσʔλ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 -
࣍ͷ࣮Λߟ͑ͯΈ·͠ΐ͏…… it "30*15+5 = 600" do expect(calc("30*15+5")).to eq 600
end ͪΐͬͱ 5 ΄ͲɺίϨ͕௨ΔΑ͏ʹߟ͑ͯΈ͍ͯͩ͘͞ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 52 -
Ͳ͏Βࠓ·ͰͷΑ͏ʹ୯७ʹϧʔϓॲཧʹͤͯܭࢉ͢ΔͷͰ͏·͘ߦ͔ ͳͦ͏Ͱ͢ɻ Ұ୴͏ͪΐͬͱ؆୯ͳςετ͔Β࢝Ίͨ΄͏͕ྑͦ͞͏Ͱ͢ɻ ͜ͷςετ 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 -
۩ମతʹͲ͏ͬͯܭࢉ͍ͯ͘͠ͷ͔ߟ͑ͯΈ·͢ɻ 30*15+5 -> 30*20 -> 600 ϓϩάϥϜͰѻ͏ࣄΛߟ͑ͯ Array ʹͯ͠
["30", "*", "15", "+", "5"] ["30", "*", "20"] ["600"] ͱܭࢉ͍ͯ͘͠Α͏ʹ͢Εྑͦ͞͏Ͱ͢ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 54 -
ͱΓ͋͑ͣ࠷ॳͷҰาΛผͷϝιουͰ࣮ͯ͠Έ·͠ΐ͏ɻ it "30*15+5 -> 30*20" do expect(_calc(["30", "*", "15",
"+", "5"])).to eq ["30", "*", "20"] end ্هΛ௨ΔΑ͏ͳίʔυΛ·ͣॻ͍ͯΈ·͠ΐ͏ɻ ʢ5 ͠ͳ͍͍ͯ͘Ͱ͢ʣ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 55 -
టष͘ॻ͍ͯΈ·ͨ͠ 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 -
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 -
͕࣍શͯ௨ΔࣄΛ֬ೝͯ͠Έ·͠ΐ͏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 -
ྫ 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 -
࠷ॳʹग़͞Ε͓ͨ ܅ͷΒͳ͍ԕ͍ੈքͷࣜΛධՁͯ͠Β͏ɻ ࣜ 4*5+6&7|8 ͷΑ͏ͳܗʹͳ͍ͬͯΔɻ ۮવʹɺԋࢉࢠͷҙຯ ruby ͷ֤ԋࢉࢠͷҙຯͱಉ͡ͳΜ͚ͩͲɺ༏ઌॱҐ ruby
ͱҟͳΔͷͰͦͷ·· eval ͢ΔΘ͚ʹ͍͔ͳ͍ɻ ԋࢉࢠͷ ༏ઌॱҐԼදͷ௨ΓɻҰԠҙຯॻ͍͚ͨͲɺҙຯ ruby ͱ ಉ͡ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 60 -
ࠓճͷ՝ 1. େ͖͍Λখׂ͘͢͞Δ 2. Λ 1 ͭͣͭղ͍͍ͯ͘ 3. ͦΕΛTDD Ͱ㕩͍ͯ͘͠
Λߦ͍·ͨ͠ɻ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 62 -
TDD ͓͞Β͍ • ςετ͕ఆ௨Γ͔֬ೝ͢Δ͜ͱ͕େࣄ • red ͕ѱ͍ɺgreen ͕ྑ͍ͷͰͳ͍ • red
ʹͳΔ͖ςετ͕ red ʹͳΓɺgreen ʹͳΔ͖ςετ͕ green ʹͳ͍ͬͯΔ͜ͱ͕େࣄ • ͦΕΛૉૣ֬͘ೝ͢ΔͨΊͷԾ࣮ • ςετίʔυͷςετʹͳΔ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 63 -
TDD ͓͞Β͍ 1. ࠷ॳʹ red ʹ͢Δ 2. ࠷Ͱ green ʹ͢Δ
• ͜͜·Ͱςετίʔυͷςετ 3. ϦϑΝΫλϦϯά͢Δ • ͕͜͜ຊؾͷ࣮ • ଟΈΜͳ͕ීஈ૾͢ΔʢେنͳʣϦϑΝΫλϦϯάͱগ͠ײ͕֮ҧ͏ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 64 -
·ͱΊ 1. େ͖͍Λখׂ͘͢͞Δ 2. Λ 1 ͭͣͭղ͍͍ͯ͘ 3. ͦΕΛ㕩͍ͯ͘͠ 1.
·ͣམͪΔςετΛॻ͘ 2. ςετ͕௨ΔҰ൪؆୯ͳ࣮Λॻ͘ʢԾ࣮ɺ໌നͳ࣮ʣ 3. ϦϑΝΫλϦϯά͢Δ 2015/08/20 TDD ͱ git Ͱ㕩Δ։ൃ 66