[英]ArgumentError wrong number of arguments (given 0, expected 1)
I have written a program for codewars that finds the lowest two integers and returns the sum of them:我为 codewars 编写了一个程序,它找到最低的两个整数并返回它们的总和:
def sum_two_smallest_numbers(numbers)
array_lowest = [0, 0]
main_iterate = 2
array_lowest[0] = sum_two_smallest_numbers[0]
array_lowest[1] = sum_two_smallest_numbers[1]
until main_iterate == sum_two_smallest_numbers.length - 1 #maybe -2, or 0
if sum_two_smallest_numbers[main_iterate] < array_lowest[0]
array_lowest[0] = sum_two_smallest_numbers[main_iterate]
main_iterate += 1
elsif sum_two_smallest_numbers[main_iterate] < array_lowest[1]
array_lowest[1] = sum_two_smallest_numbers[main_iterate]
main_iterate += 1
else
main_iterate += 1
end
end
return array_lowest[0] + array_lowest[1]
end
to fulfill tests as follows:完成以下测试:
Test.assert_equals(sum_two_smallest_numbers([5, 8, 12, 18, 22]), 13)
Test.assert_equals(sum_two_smallest_numbers([7, 15, 12, 18, 22]), 19)
Test.assert_equals(sum_two_smallest_numbers([25, 42, 12, 18, 22]), 30)
It complains about my first line (this was provided along with an end
), and if I replace numbers
with any actual numbers, as in the test cases, it throws this:它抱怨我的第一行(这是用提供沿end
),如果我更换numbers
与任何实际数字,因为在测试情况下,抛出这个:
syntax error, unexpected tINTEGER, expecting ')'
How can I solve this?我该如何解决这个问题?
You're recursively calling sum_two_smallest_numbers
with no arguments., and it requires an argument.你递归调用sum_two_smallest_numbers
没有参数。,它需要一个参数。 Each time you write sum_two_smallest_numbers
, that's a method invocation.每次编写sum_two_smallest_numbers
,都是一个方法调用。 When you write sum_two_smallest_numbers[0]
, that's a method invocation with no arguments, the [0]
would access the 0'th element of the returned value, if the invocation succeeded.当您编写sum_two_smallest_numbers[0]
,这是一个没有参数的方法调用,如果调用成功, [0]
将访问返回值的第 0 个元素。
It seems like you might have wanted numbers[0]
, sum_two_smallest_numbers[0]
.看起来您可能想要numbers[0]
, sum_two_smallest_numbers[0]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.