Peña, Botp
9/30/2006 2:13:00 AM
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