Ruby 程序在repl.it中工作,但得到;“未定义的方法”;代码战中的错误
我一直在处理codewars挑战,我想提交它,我的代码在repl.it中工作,并通过了所有测试,但当我在codewars shell中运行它时,我得到以下错误:Ruby 程序在repl.it中工作,但得到;“未定义的方法”;代码战中的错误,ruby,Ruby,我一直在处理codewars挑战,我想提交它,我的代码在repl.it中工作,并通过了所有测试,但当我在codewars shell中运行它时,我得到以下错误: queue_time': undefined method `max' for nil:NilClass (NoMethodError) from `block in' from `block in describe' from `measure' from `describe' from
queue_time': undefined method `max' for nil:NilClass (NoMethodError)
from `block in'
from `block in describe'
from `measure'
from `describe'
from `'
代码如下:
def queue_time(customers, n)
total_queue = []
i = 0
while i < n
total_queue << [customers[i]]
i += 1
end
open_queue_index = total_queue.index {|x| x == total_queue.min}
k = n
while k < customers.length
array_summed = []
total_queue[open_queue_index] << customers[k]
total_queue.each_index do |index|
array_summed << total_queue[index].reduce(:+)
end
open_queue_index = array_summed.index {|x| x == array_summed.min}
k += 1
end
array_summed.max
end
def队列时间(客户,n)
总队列=[]
i=0
而我
总队列未初始化
while false
array_summed = [] # initialized
end
array_summed # is nil because it was never initialized.
```如果customers
数组为空,您的方法将中断。如果该数组为空,则该方法将永远不会输入,而k
块和array\u summated
将永远不会初始化
while false
array_summed = [] # initialized
end
array_summed # is nil because it was never initialized.
然后在最后一行,array\u summated.max
基本上类似于说nil.max
您是否在repl.it和codewars中传递不同的客户数组?如果您在codewars中测试的阵列为空,则该阵列将中断。否,它不在范围之外。如果是,您将得到一个名称错误@JörgWMittag谢谢您的更正。更改为“未初始化”,我从repl.it复制并粘贴到codewars。我在开始时添加了一些代码,以说明客户为空或n==0,现在我得到了以下错误:each':数组与数组的比较失败(ArgumentError)from
min'fromblock in queue\u time'from
index'fromqueue\u time'from
block in'fromblock in in'description'from
测量'fromdescripe'from
'这样做有效吗?如果没有,请将代码添加到您的问题中,以便我们可以看到它?