Out of range problems and odd number hashs

Hi,
I am building a program that calculates the data points of a circle
based on 1 data point that the user enters. The user could enter the
radius circumference or area and get back the other properties of the
circle. I’m getting a ‘Circles.rb:5: warning: Float 3.14 out of range’
in this context;

value1 = gets.chomp.to_f
puts 'The circle is of radius ’ + [(value1/3.14)/2].to_s + ‘units’

I’ve had this problem before. Another problem is a ‘odd number list for
hash’ error in this code. It happened 4 times in the code;

…ea ’ + {{[(value1/3.14)/2]*2} 3.14}.to_s + ’ square units’
^

…{{[(value1/3.14)/2]*2} 3.14}.to_s + ’ square units’
^

…{[math.sqrt(value3/3.14)] * 2}.to_s + ’ units, and circumfer…
^

…{[math.sqrt(value3/3.14)] * 2} * 3.14).to_s + ’ units.’
^

what should I do?

On Thu, Mar 25, 2010 at 12:47 PM, Charlie Ca [email protected]
wrote:

Hi,
I am building a program that calculates the data points of a circle
based on 1 data point that the user enters. The user could enter the
radius circumference or area and get back the other properties of the
circle. I’m getting a ‘Circles.rb:5: warning: Float 3.14 out of range’
in this context;

value1 = gets.chomp.to_f
puts 'The circle is of radius ’ + [(value1/3.14)/2].to_s + ‘units’

The following works for me (I changed the 2 to 2.0 and used string
interpolation which calls to_s for you, also you don’t need and
array):

irb(main):010:0> value1 = 100.0
=> 100.0
irb(main):012:0> puts “The circle is of radius #{(value1/3.14)/2.0}
units”
The circle is of radius 15.9235668789809 units

Which number are you entering?

I’ve had this problem before. Another problem is a ‘odd number list for
hash’ error in this code. It happened 4 times in the code;

…ea ’ + {{[(value1/3.14)/2]*2} 3.14}.to_s + ’ square units’

Your usage of { and [ is kind of weird, I think you might be using it
to specify precedence as you do in mathematics, but in Ruby they are
used for very different things, namely creating hash and array
literals. For precedence you should use only parenthesis:

irb(main):013:0> ((((value1/3.14)/2)*2) 3.14)
=> 796.178343949044

(you probably also don’t need so many parens, anyway).

irb(main):018:0> (value1/3.14/2)*2 3.14
=> 796.178343949044

although if you think they make the expresion clearer go for them.

what should I do?

Another completely different suggestion after fixing the above: if you
have the same expression many times (more than 1) refactor it to a
method.

Jesus.

Can we see more context for this code. For the life of me I can’t see
why
you have and ‘{’ or '}'s in you code at that point.

Additionally

puts 'The circle is of radius ’ + [(value1/3.14)/2].to_s + ‘units’

Can be rewritten as

puts “The circle is of radius #{(value1/3.14)/2} units”

Converting things to a list just to be able to convert them to a string
is
‘code smell’. If you are calling the to_X methods all over the place you
are
probably doing something wrong. Image someone pointing at you code and
saying “why did you do that?”. If your answer is something like “dunno,
it
works” then you have a problem.

Ruby code tends to be simpler and shorter than other languages. You will
often find yourself saying “what! is that it?”

2010/3/25 Peter H. [email protected]:

Additionally

puts 'The circle is of radius ’ + [(value1/3.14)/2].to_s + ‘units’

Btw, no need for “3.14” - there is

irb(main):001:0> Math::PI
=> 3.14159265358979

Kind regards

robert