我正在尝试从数组中打印出一个值,但由于某些原因不能。 这是我的代码:
click = rightClick(mat, sel) #(click is a method that opens an input window. It returns an array of 5 values)
constLib << click
constNum=constLib.length
for i in 0..constNum
puts (constLib[i][1])
end
我收到一条错误消息:
Error: #<NoMethodError: undefined method `[]' for nil:NilClass>
(eval):37:in `block (4 levels) in initialize'
(eval):34:in `each'
(eval):34:in `block (3 levels) in initialize'
-e:1:in `call'
有什么想法吗?
谢谢!
最佳答案
..
是包容性的。这意味着,您正在访问越界索引,即数组的长度。因此将其更改为 ...
这是排他性的。
然而,这不是 rubyists 处理它的方式,因为他们可能会对您使用 for 循环不悦。最好使用each
constLib.each {|arr| puts arr[1]}
关于Ruby - 从数组中打印一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28234349/