Ruby 数组不使用模运算符

Ruby 数组不使用模运算符,ruby,arrays,Ruby,Arrays,第一个代码可以工作,但我不明白为什么第二个不能。如有任何见解,将不胜感激。我知道在这个例子中我真的不需要数组,我只是为了学习而让它工作 def stamps(input) if input % 5 == 0 puts 'Zero!' else puts 'NO!' end end print stamps(8) 但这不起作用: array_of_numbers = [8] def stamps(input_array) if input_array % 5

第一个代码可以工作,但我不明白为什么第二个不能。如有任何见解,将不胜感激。我知道在这个例子中我真的不需要数组,我只是为了学习而让它工作

def stamps(input)
  if input % 5 == 0
    puts 'Zero!'
  else
    puts 'NO!'
  end
end

print stamps(8)
但这不起作用:

array_of_numbers = [8]

def stamps(input_array)
  if input_array % 5 == 0
    puts 'Zero!'
  else
    puts 'NO!'
  end
end

print stamps(array_of_numbers)

因为输入数组是数组,8是数字。使用
first
检索数组的第一个元素

array_of_numbers = [8]

def stamps(input_array)
  if input_array.first % 5 == 0
    puts 'Zero!'
  else
    puts 'NO!'
  end
end

print stamps(array_of_numbers)

当输入为数字或数组时,以下函数起作用:

def stamps(input)
  input = [input] unless input.is_a?(Array)
  if input.first % 5 == 0
    puts 'Zero!'
  else
    puts 'NO!'
  end
end

没有为数组定义
%
方法。您希望第二个示例如何工作?那么,您希望第二个示例中的代码做什么?如果数组中既有8又有5呢?那么你希望你的代码做什么呢?我为我的无知道歉,谢谢你们给我指出了我的错误,我很感激!