# Interesting irb experiment

**URL:** <https://www.ruby-forum.com/t/interesting-irb-experiment/196096>\
**Category:** Ruby\
**Created:** [October 6, 2010, 8:18pm UTC](https://www.ruby-forum.com/t/interesting-irb-experiment/196096 "2010-10-06T20:18:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaShiellS\_Jude\_TSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@DaShiellS\_Jude\_TSS](https://www.ruby-forum.com/u/DaShiellS_Jude_TSS)\
**Post date:** [October 6, 2010, 8:18pm UTC](https://www.ruby-forum.com/t/interesting-irb-experiment/196096/1 "2010-10-06T20:18:15Z")

</div>

J=126_rand+33.round  
K=j.chr  
Works as far as it goes, however  
J=126_rand+33.round.chr  
Throws an error. The actual error message seems strange after .round  
did its work.

---

<div class="post-metadata">

**Author:** ![DaShiellS\_Jude\_TSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@DaShiellS\_Jude\_TSS](https://www.ruby-forum.com/u/DaShiellS_Jude_TSS)\
**Post date:** [October 6, 2010, 8:27pm UTC](https://www.ruby-forum.com/t/interesting-irb-experiment/196096/2 "2010-10-06T20:27:57Z")

</div>

DaShiell, Jude T. CIV NAVAIR 1490, 1, 26 wrote:

> J=126_rand+33.round  
> K=j.chr  
> Works as far as it goes, however  
> J=126_rand+33.round.chr  
> Throws an error. The actual error message seems strange after .round  
> did its work.

You’re not showing actual copy-pasted code nor error message, but at a  
guess I’d say you need something like

j=((126\*rand+33).round.chr

Using rand(126) would avoid the need for round

---

<div class="post-metadata">

**Author:** ![DaShiellS\_Jude\_TSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@DaShiellS\_Jude\_TSS](https://www.ruby-forum.com/u/DaShiellS_Jude_TSS)\
**Post date:** [October 6, 2010, 8:39pm UTC](https://www.ruby-forum.com/t/interesting-irb-experiment/196096/3 "2010-10-06T20:39:19Z")

</div>

On Wednesday 06 October 2010, DaShiell, Jude T. CIV NAVAIR 1490, 1, 26  
wrote:

> |J=126_rand+33.round  
> |K=j.chr  
> |Works as far as it goes, however  
> |J=126_rand+33.round.chr  
> |Throws an error. The actual error message seems strange after .round  
> |did its work.

I don’t know what you want to obtain. If you want the character with  
code  
126\*rand + 33 (rounded), then you need to put some parentheses. I think  
there  
are two issues with your code. One is the direct cause of the error you  
see  
(by the way, it would have been better to post it, rather than only  
saying  
it’s strange): the chr method is called on the value returned by  
33.round,  
returning “!”. So, your code evaluates to:

J=126\*rand + “!”

Since you can’t add a string to a number, you get the error. To obtain  
the  
correct result, you should put everything from 126 to round in  
parentheses:

J=(126\*rand + 33.round).chr

The second issue regards 33.round. Since 33 is an integer, 33.round will  
still  
give you 33. What I think you wanted is to call round on the result of  
the  
sum. It’s the same kind of mistake I mentioned previously, and it’s  
solved in  
the same way, by putting parentheses:

J=(126\*rand + 33).round.chr

By the way, note that you can generate a random integer between 0 and n  
by  
calling rand(n), so your code coud be rewritten more simply as:

J=(rand(126) + 33).chr

I hope this helps

Stefano
