# Out of range problems and odd number hashs

**URL:** <https://www.ruby-forum.com/t/out-of-range-problems-and-odd-number-hashs/185740>\
**Category:** Ruby\
**Created:** [March 25, 2010, 12:46pm UTC](https://www.ruby-forum.com/t/out-of-range-problems-and-odd-number-hashs/185740 "2010-03-25T12:46:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ceissen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@ceissen](https://www.ruby-forum.com/u/ceissen)\
**Post date:** [March 25, 2010, 12:46pm UTC](https://www.ruby-forum.com/t/out-of-range-problems-and-odd-number-hashs/185740/1 "2010-03-25T12:46:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ceissen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@ceissen](https://www.ruby-forum.com/u/ceissen)\
**Post date:** [March 25, 2010, 1:01pm UTC](https://www.ruby-forum.com/t/out-of-range-problems-and-odd-number-hashs/185740/2 "2010-03-25T13:01:57Z")

</div>

On Thu, Mar 25, 2010 at 12:47 PM, Charlie Ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![ceissen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@ceissen](https://www.ruby-forum.com/u/ceissen)\
**Post date:** [March 25, 2010, 1:06pm UTC](https://www.ruby-forum.com/t/out-of-range-problems-and-odd-number-hashs/185740/3 "2010-03-25T13:06:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ceissen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@ceissen](https://www.ruby-forum.com/u/ceissen)\
**Post date:** [March 25, 2010, 1:27pm UTC](https://www.ruby-forum.com/t/out-of-range-problems-and-odd-number-hashs/185740/4 "2010-03-25T13:27:42Z")

</div>

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?”

---

<div class="post-metadata">

**Author:** ![ceissen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@ceissen](https://www.ruby-forum.com/u/ceissen)\
**Post date:** [March 25, 2010, 1:30pm UTC](https://www.ruby-forum.com/t/out-of-range-problems-and-odd-number-hashs/185740/5 "2010-03-25T13:30:38Z")

</div>

2010/3/25 Peter H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
