我写了经典的fizzbuzz代码。为什么这个红宝石使用 u001 for 1以及如何更改?
该规范:
describe "can determine fizzbuxx output for" do
it "3" do
expect(Fizzbuzz.nums(3)).to eq "123fizz"
end
end
代码:
class Fizzbuzz
def self.nums(n)
result=""
(1..n).each do |inner|
puts "inner #{inner}"
result << inner
if (inner % 3)==0
result << 'fizz'
end
if (inner % 5)==0
result << 'buzz'
end
end
result
end
end
为什么我收到\u001
...而不是1
?
Failures:
1) can determine fizzbuxx output for 3
Failure/Error: expect(Fizzbuzz.nums(3)).to eq "123fizz"
expected: "123fizz"
got: "\u0001\u0002\u0003fizz"
(compared using ==)
# ./fizzbuzz_spec.rb:5:in `block (2 levels) in <top (required)>'
它是某种类型的utf-8问题?
可能与您用事实做''<<用'Fixnum'。将其更改为'result << inner.to_s'将产生所需的结果。我相信有人可以挖掘出确切的行为并提供更好的解释。 – 2014-10-16 12:43:08
是的,工作。作为回答发布,我会接受。 thx – 2014-10-16 12:44:59
从['String#<<'](http://www.ruby-doc.org/core-2.1.3/String.html#method-i-3C-3C):*“将给定对象连接到str。如果对象是一个Integer,它被认为是一个代码点,并在连接之前转换为一个字符。“* – Stefan 2014-10-16 12:46:08