[英]Ruby undefined method '+' for nil:NilClass
Why is this not a valid operation?为什么这不是一个有效的操作?
def get_highest_bar()
#convert string to integer array
data = @data.split(",")
return Integer(data.max)
end
#rounds up to nearest factor of 100
def round_up(n)
return 100 if n < 100
return (n+50)/100*100
end
@axis_range_prefix = "chxr="
@y_axis_index = "1"
#error here:
axis_range = @axis_range_prefix + [@y_axis_index, "0", highest_bar.to_s()].join(",")
You don't need the get_ prefix in your get_highest_bar method.您的 get_highest_bar 方法中不需要 get_ 前缀。 That's a java habit, isn't it?
这是 java 的习惯,不是吗? The fact that you called it highest_bar later proves that a good name reflects what the result is, not the action you took to get it.
你后来称它为highest_bar 的事实证明,一个好名字反映的是结果,而不是你为得到它而采取的行动。
Parens after the method definition are optional and not idiomatic ruby.方法定义后的括号是可选的,不是惯用的 ruby。
The return Integer(data.max)
probably doesn't do what you think. return Integer(data.max)
可能不会像您想的那样。 If @data
contained "1,10,2" the max is 2, because they're being compared as strings.如果
@data
包含“1,10,2”,则最大值为 2,因为它们被作为字符串进行比较。
Rewritten method:改写方法:
def highest_bar
@data.split(",").map(&:to_i).max
end
You have a typo, highest_bar is undefined.你有一个错字, highest_bar是未定义的。 You should call get_highest_bar ().
你应该调用get_highest_bar ()。 Ie
IE
axis_range = @axis_range_prefix + [@y_axis_index, "0", get_highest_bar.to_s()].join(",")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.