简体   繁体   English

ArgumentError 错误的参数数量(给定 0,预期为 1)

[英]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.

相关问题 参数数量错误(给定1,预期为2)(ArgumentError) - wrong number of arguments (given 1, expected 2) (ArgumentError) Jekyll:参数数量错误(给定 2,预期为 1)(ArgumentError) - Jekyll: wrong number of arguments (given 2, expected 1) (ArgumentError) ArgumentError:参数数量错误(给定0,应为1..2) - ArgumentError: wrong number of arguments (given 0, expected 1..2) &#39;initialize&#39;:错误的参数数量(给定3个,预期为0)(ArgumentError) - `initialize': wrong number of arguments (given 3, expected 0) (ArgumentError) Rails 中的 ArgumentError(arguments 的编号错误(给定 5,预期为 1)) - ArgumentError (wrong number of arguments (given 5, expected 1)) in Rails Rails-ArgumentError(参数数量错误(给定1,预期为0)): - Rails - ArgumentError (wrong number of arguments (given 1, expected 0)): Ruby - ArgumentError:参数数量错误(给定 3,预期为 2) - Ruby - ArgumentError: wrong number of arguments (given 3, expected 2) `initialize': arguments 的错误编号(给定 0,预期为 2)(ArgumentError) - `initialize': wrong number of arguments (given 0, expected 2) (ArgumentError) ArgumentError:参数数量错误(给定 0,预期为 1)Ruby - ArgumentError: wrong number of arguments (given 0, expected 1) Ruby ArgumentError:参数数量错误(给定 0,预期为 1) - Ruby - ArgumentError: wrong number of arguments (given 0, expected 1) - Ruby
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM