1

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(",")

2 Answers 2

3
  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
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! Java tendencies die hard. Does the .map(&:to_i) portion feed every value in the list to the to_i() function? What do the '&' and ':' mean? Sorry I am new to ruby.
It's shorthand for .map{|x| x.to_i}
When I try to do @data.split(",").map(&:to_i).max I receive an error: 'wrong argument type Symbol (expected Proc)'
I see, that shortcut only works 1.8.6+ the company I work for only has 1.8.4
Really? That's pretty ancient. The company had better start thinking about an upgrade plan.
|
0

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(",")

1 Comment

highest_bar is the name of a local variable, which after looking at what I posted i forgot to include. My mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.