[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

not equal? "5".to_i+ "6".to_i and "5".to_i +"6".to_i

cap

2/4/2007 1:40:00 PM

ruby 1.8.5 (2006-12-25 patchlevel 12) [i686-linux]


>> "5".to_i+ "6".to_i
=> 11
>> "5".to_i +"6".to_i
=> 5


maybe it's a mistak?

4 Answers

Rimantas Liubertas

2/4/2007 1:58:00 PM

0

> >> "5".to_i+ "6".to_i
> => 11
> >> "5".to_i +"6".to_i
> => 5

The second case +"6".to_i is treated as an argument for to_i method.
This will make it more obvious:
>> "111".to_i +"2".to_i
=> 7

"111".to_i +"2".to_i is equivalent to "111".to_i(2), hence the result.


Regards,
Rimantas
--
http://rim...

Eric Jacoboni

2/4/2007 2:12:00 PM

0

"cap" <capitain@gmail.com> writes:

> ruby 1.8.5 (2006-12-25 patchlevel 12) [i686-linux]
>
>
>>> "5".to_i+ "6".to_i
> => 11

so, you're calling "5".to_i.+("6".to_i), ie. "5".to_i.+(6),
ie. 5.+(6), ie. 11

>>> "5".to_i +"6".to_i

you're calling "5".to_i(+"6".to_i), ie. "5".to_i(6),
ie. "5" converted in base 6, ie 5

Rule of thumb : always put spaces around operators...

--

Eric Jacoboni

2/4/2007 2:16:00 PM

0

Eric Jacoboni <eric.jacoboni@free.fr> writes:


> you're calling "5".to_i(+"6".to_i), ie. "5".to_i(6),
> ie. "5" converted in base 6, ie 5

Ooops... i wanted to say "5" in base 6, converted to int...

--


cap

2/5/2007 1:35:00 PM

0

On Feb 4, 10:12 pm, Eric Jacoboni <eric.jacob...@free.fr> wrote:
> "cap" <capit...@gmail.com> writes:
> > ruby 1.8.5 (2006-12-25 patchlevel 12) [i686-linux]
>
Thanks very much!
I got it
> >>> "5".to_i+ "6".to_i
> > => 11
>
> so, you're calling "5".to_i.+("6".to_i), ie. "5".to_i.+(6),
> ie. 5.+(6), ie. 11
>
> >>> "5".to_i +"6".to_i
>
> you're calling "5".to_i(+"6".to_i), ie. "5".to_i(6),
> ie. "5" converted in base 6, ie 5
>
> Rule of thumb : always put spaces around operators...
>
> --