为什么这不是一个有效的操作?
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(",")
最佳答案
您不需要在 get_highest_bar 方法中使用 get_ 前缀。这是 Java 的习惯,不是吗?您后来将其命名为 highest_bar 的事实证明,好的名称反射(reflect)的是结果,而不是您为获得结果所采取的行动。
方法定义后的括号是可选的,不是惯用的 ruby。
return Integer(data.max)
可能并不如您所想。如果@data
包含“1,10,2”,则最大值为 2,因为它们是作为字符串进行比较的。
重写方法:
def highest_bar
@data.split(",").map(&:to_i).max
end
关于Ruby 未定义方法 '+' 为 nil :NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6795427/