dir = File.expand_path(File.dirname(__FILE__)) @_read = ->(fn){ File.open(fn).map{ |r| r.chomp }} @answer = @_read[File.join(dir, 'fizz_buzz.answer')] end it 'should be eauql to anser' do fn = 'fizz_buzz.result' fizzbuzz(100, fn) expect(@_read[fn]).to match_array @answer end end 20 / 28
|i| f.puts case when i % 15 == 0 then 'FizzBuzz' when i % 3 == 0 then 'Fizz' when i % 5 == 0 then 'Buzz' else i end end end end 文字列演算が重いので、 場合分けに変更 23 / 28
} { 3 => 'Fizz', 5 => 'Buzz', }.each do |no, key| no.step(14,no){ |i| CONDITION[i] = key } end def fizzbuzz(n, fn) FizzBuzz.fizzbuzz(n, fn) end class << self public def fizzbuzz(n, fn) File.open(fn,'w') do |f| 1.upto(n) do |i| f.puts CONDITION[i % 15] || i end end end end end 24 / 28