当我遵循 Ruby rubocop 风格指南并使用 .zero 时方法失败?而不是== 0

Method fail when I follow Ruby rubocop style guide and use .zero? instead of == 0

我正在尝试完成 Ruby 中的编码练习,如下所示:

TODO: starting with an array of integers, return an array with integers and 'Fizz', 'Buzz' or 'FizzBuzz'

Write a method fizz_buzz, which takes a number as an argument, and return an array of number elements from 1 to number, but replaces some of them according to these rules:

  • If the number is divisible by 3, then replace it by 'Fizz'
  • If the number is divisible by 5, then replace it by 'Buzz'
  • If the number is divisible by both 3 and 5, then replace it by 'FizzBuzz'Write

如果我按照 rubocop style guide,我们被要求在完成任务后使用它,并且按照指示使用 .zero 而不是 == 0?,我的方法失败了,我不明白为什么。

我的风格指南要求的编辑现在失败的解决方案:

def fizz_buzz(number)
  fail ArgumentError, "#{number} is less than 1" if number < 1
  a = [number]
  while number > 1
    number = number - 1
    a.unshift(number)
    a.map! { |x| (x % 15).zero? ? 'FizzBuzz' : x }
    a.map! { |x| (x % 3).zero? ? 'Fizz' : x }
    a.map! { |x| (x % 5).zero? ? 'Buzz' : x }
  end
  a
end

  should return the array [ 1, 2, 'Fizz' ] for number 3 (FAILED - 1)
  should return the array [ 1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7 ] for number 7 (FAILED - 2)
  should return an array with 'FizzBuzz' at the 15th element of the array (15 is divisible by both 3 and 5) (FAILED - 3)

失败:

  1) fizz_buzz should return the array [ 1, 2, 'Fizz' ] for number 3
     Failure/Error: a.map! { |x| (x % 5).zero? ? 'Buzz' : x }

 NoMethodError:
   undefined method `zero?' for "Fizz":String
 # ./lib/fizz_buzz.rb:12:in `block in fizz_buzz'
 # ./lib/fizz_buzz.rb:12:in `map!'
 # ./lib/fizz_buzz.rb:12:in `fizz_buzz'
 # ./spec/fizz_buzz_spec.rb:13:in `block (2 levels) in <top (required)>'

  2) fizz_buzz should return the array [ 1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7 ] for number 7
     Failure/Error: a.map! { |x| (x % 5).zero? ? 'Buzz' : x }

 NoMethodError:
   undefined method `zero?' for "Fizz":String
 # ./lib/fizz_buzz.rb:12:in `block in fizz_buzz'
 # ./lib/fizz_buzz.rb:12:in `map!'
 # ./lib/fizz_buzz.rb:12:in `fizz_buzz'
 # ./spec/fizz_buzz_spec.rb:17:in `block (2 levels) in <top (required)>'

  3) fizz_buzz should return an array with 'FizzBuzz' at the 15th element of the array (15 is divisible by both 3 and 5)
     Failure/Error: a.map! { |x| (x % 5).zero? ? 'Buzz' : x }

 NoMethodError:
   undefined method `zero?' for "Fizz":String
 # ./lib/fizz_buzz.rb:12:in `block in fizz_buzz'
 # ./lib/fizz_buzz.rb:12:in `map!'
 # ./lib/fizz_buzz.rb:12:in `fizz_buzz'
 # ./spec/fizz_buzz_spec.rb:21:in `block (2 levels) in <top (required)>'

我的工作解决方案未通过样式指南:

def fizz_buzz(number)
  fail ArgumentError, "#{number} is less than 1" if number < 1
  a = [number]
  while number > 1
    number = number - 1
    a.unshift(number)
    a.map! { |x| (x % 15) == 0 ? 'FizzBuzz' : x }
    a.map! { |x| (x % 3) == 0 ? 'Fizz' : x }
    a.map! { |x| (x % 5) == 0 ? 'Buzz' : x }
  end
  a
end

分配的任务提供的解决方案:

# def fizz_buzz(number)
#   fail ArgumentError, "#{number} should be greater than 1" if number < 1
#   (1..number).map do |i|
#     if (i % 3).zero? && (i % 5).zero?
#       'FizzBuzz'
#     elsif (i % 3).zero?
#       'Fizz'
#     elsif (i % 5).zero?
#       'Buzz'
#     else
#       i
#     end
#   end
# end

既然我们看到了失败的代码,那么失败的原因如下:

您有一个数组 a,我想您打算在其中包含结果。您将数字添加到它前面,并修复需要说 "Fizz" 或 "Buzz" 或 "FizzBuzz" 的数字。这可行,但有一个严重的失败:您在循环的每次迭代中处理数组 。如果您只修复一次,就在 return 之前修复它,那就没问题了。但是,这样,会发生什么(例如输入值 4):

  • 你的数组开始于 4
  • 你运行修复了,但是4很好,不需要修复
  • 你在前面加上3
  • 你运行修复;该数组现在是 ["Fizz", 4]。到目前为止一切顺利。
  • 你在前面加上2;数组是 [2, "Fizz", 4]
  • 您 运行 修复。 2 无人干预通过,然后轮到 "Fizz"
  • "Fizz" 不是数字,因此您不会调用求余运算符 Number#%,而是调用格式化运算符 String#%。字符串 "Fizz" 中没有格式符号,因此格式化运算符没有做任何有趣的事情,并且 returns 字符串 "Fizz".
  • 然后你测试它是否为零。当您尝试 "Fizz" == 0 时,它本来是 "Duh, no" 并且只是 returned false,因为所有 类(从 BasicObject 开始)定义 #==。但是 "Fizz".zero? 失败了,因为与 Number#zero? 不同,String#zero? 不是一个东西。