"1".to_i *2 == 1 && "1".to_i*2 == 2?

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

On 9/30/06, Giovanni I. [email protected] 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

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

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

‘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

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

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.

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. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com