# "1".to\_i \*2 == 1 && "1".to\_i\*2 == 2?

**URL:** <https://www.ruby-forum.com/t/1-to-i-2-1-1-to-i-2-2/70552>\
**Category:** Ruby\
**Created:** [September 30, 2006, 1:48am UTC](https://www.ruby-forum.com/t/1-to-i-2-1-1-to-i-2-2/70552 "2006-09-30T01:48:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 30, 2006, 1:48am UTC](https://www.ruby-forum.com/t/1-to-i-2-1-1-to-i-2-2/70552/1 "2006-09-30T01:48:48Z")

</div>

Can anyone explain to me why

“1”.to\_i\*2 is equal to 2  
and  
“1”.to\_i \*2 is equale to 1?

Thanks  
Giovanni

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 30, 2006, 1:56am UTC](https://www.ruby-forum.com/t/1-to-i-2-1-1-to-i-2-2/70552/2 "2006-09-30T01:56:49Z")

</div>

On 9/30/06, Giovanni I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anyone explain to me why
> 
> “1”.to\_i\*2 is equal to 2

this is (“1”.to\_i) \* ( 2 )

and

> “1”.to\_i \*2 is equale to 1?

this is “1”.to\_i(\*2)

Thanks

Non c’Ã¨ di che.

Giovanni

> 

## Robert

Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 30, 2006, 1:58am UTC](https://www.ruby-forum.com/t/1-to-i-2-1-1-to-i-2-2/70552/3 "2006-09-30T01:58:49Z")

</div>

Giovanni I. wrote:

> Can anyone explain to me why
> 
> “1”.to\_i\*2 is equal to 2  
> and  
> “1”.to\_i \*2 is equale to 1?
> 
> Thanks  
> Giovanni

It seems to be interpreting

“1”.to\_i \*2

as

“1”.to\_i(\*2)

or

“1”.to\_i(2)

which is 1. Remember the \* in function calls is the unarray operator.  
Kinda weird in this case, though.

-Justin

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 30, 2006, 2:04am UTC](https://www.ruby-forum.com/t/1-to-i-2-1-1-to-i-2-2/70552/4 "2006-09-30T02:04:50Z")

</div>

because the second version converts the given string in base 2.

to\_i accept an optional parameter which is the base of the integer (by  
default 10)  
[http://www.ruby-doc.org/core/classes/String.html#M001463](http://www.ruby-doc.org/core/classes/String.html#M001463)

‘1’.to\_i\*2 calls the method \* applied to the value returned by to\_i  
‘1’.to\_i \*2 is like ‘1’.to\_i(\*2)  
anyway a more common way to write the expression is

‘1’.to\_i \* 2

Paolo

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 30, 2006, 4:14am UTC](https://www.ruby-forum.com/t/1-to-i-2-1-1-to-i-2-2/70552/5 "2006-09-30T04:14:56Z")

</div>

fr Giovanni:

# “1”.to\_i\*2 is equal to 2

# and

# “1”.to\_i \*2 is equale to 1?

careful w #to\_i. It’s a powerful converter method that accepts a  
parameter for the needed conversion.

irb(main):021:0\> “111”.to\_i 2 # base 2 conversion  
=\> 7  
irb(main):022:0\> “111”.to\_i \* 2 # base 10 (default) conversion mult  
by 2  
=\> 222  
irb(main):023:0\> “111”.to\_i 2 # base 2  
=\> 7  
irb(main):024:0\> “111”.to\_i(2) # base 2  
=\> 7  
irb(main):025:0\> “111”.to\_i(_2) # base 2  
=\> 7  
irb(main):026:0\> “111”.to\_i \* 2 # base 10 times 2  
=\> 222  
irb(main):027:0\> “111”.to\_i_2 # base 10 times 2  
=\> 222  
irb(main):028:0\> “111”.to\_i \*2 # base 2  
=\> 7  
irb(main):029:0\> “111”.to\_i 3 # base 3  
=\> 13

## C:\family\ruby\outlook\>ri -T String#to\_i ------------------------------------------------------------ String#to\_i str.to\_i(base=10) =\> integer

```
 Returns the result of interpreting leading characters in _str_ as
 an integer base _base_ (2, 8, 10, or 16). Extraneous characters
 past the end of a valid number are ignored. If there is not a valid
 number at the start of _str_, +0+ is returned. This method never
 raises an exception.

    "12345".to_i #=> 12345
    "99 red balloons".to_i #=> 99
    "0a".to_i #=> 0
    "0a".to_i(16) #=> 10
    "hello".to_i #=> 0
    "1100101".to_i(2) #=> 101
    "1100101".to_i(8) #=> 294977
    "1100101".to_i(10) #=> 1100101
    "1100101".to_i(16) #=> 17826049

```

btw, i’m dumb today in windows. i can’t run “ri -T String#to\_i” in irb.  
Tips pls.  
C:\family\ruby\outlook\>ruby -v  
ruby 1.8.5 (2006-08-25) [i386-mswin32]  
C:\family\ruby\outlook\>ver  
Microsoft Windows XP [Version 5.1.2600]

kind regards -botp

# Thanks

# Giovanni

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 30, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/1-to-i-2-1-1-to-i-2-2/70552/6 "2006-09-30T19:04:43Z")

</div>

Collins, Justin wrote:

> Remember the \* in function calls is the unarray operator. Kinda weird in this case, though.

Reminds me that for Ruby 2, the splat operator was supposed to be a  
little more predictable. Anything of that in Ruby 1.9? Can’t recall  
anything on the change sumamry page about that. This sounds like a  
rather nasty gotcha to the people that are lazy about the  
one-space-around-operators style convention.

David V.

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 30, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/1-to-i-2-1-1-to-i-2-2/70552/7 "2006-09-30T21:39:03Z")

</div>

On Sep 29, 2006, at 4:47 PM, Giovanni I. wrote:

> Can anyone explain to me why
> 
> “1”.to\_i\*2 is equal to 2  
> and  
> “1”.to\_i \*2 is equale to 1?

This is why you should run with -w as often as possible:

$ ruby -w  
“1”.to\_i _2  
-:1: warning: `_’ interpreted as argument prefix

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
