开发者

Ruby undefined method '+' for nil:NilClass

开发者 https://www.devze.com 2023-03-22 13:47 出处:网络
Why is this not a valid operation? def get_highest_bar() #co开发者_如何学Gonvert string to integer array

Why is this not a valid operation?

def get_highest_bar()
   #co开发者_如何学Gonvert 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(",")


  1. You don't need the get_ prefix in your get_highest_bar method. That's a java habit, isn't it? 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.

  2. Parens after the method definition are optional and not idiomatic ruby.

  3. The return Integer(data.max) probably doesn't do what you think. If @data contained "1,10,2" the max is 2, because they're being compared as strings.

Rewritten method:

def highest_bar
   @data.split(",").map(&:to_i).max
end


You have a typo, highest_bar is undefined. You should call get_highest_bar(). I.e.

 axis_range = @axis_range_prefix + [@y_axis_index, "0", get_highest_bar.to_s()].join(",")
0

精彩评论

暂无评论...
验证码 换一张
取 消