From: Michael W. Ryder [mailto:
[email protected]]=20
# I am trying to figure out why methods that seem to do the same thing=20
# behave so differently. For example Integer("0377") returns 255 as=20
# expected for an octal number but "0377".to_i returns 377. Or why=20
# Integer("12e3") crashes while Float("12e3") returns 12000.0=20
# as expected.=20
# I was hoping to find somewhere that tells when to use one=20
# or the other=20
# method depending upon expected inputs. It is very time consuming to=20
# have to keep testing a method to find out it's limitations.
qri and irb are your friends.
eg,
botp@botp-desktop:~$ qri -f plain integer float string.to_i
--------------------------------------------------------- Kernel#Integer
Integer(arg) =3D> integer
------------------------------------------------------------------------
Converts _arg_ to a +Fixnum+ or +Bignum+. Numeric types are
converted directly (with floating point numbers being truncated).
If _arg_ is a +String+, leading radix indicators (+0+, +0b+, and
+0x+) are honored. Others are converted using +to_int+ and +to_i+.
This behavior is different from that of +String#to_i+.
Integer(123.999) #=3D> 123
Integer("0x1a") #=3D> 26
Integer(Time.new) #=3D> 1049896590
----------------------------------------------------------- Kernel#Float
Float(arg) =3D> float
------------------------------------------------------------------------
Returns _arg_ converted to a float. Numeric types are converted
directly, the rest are converted using _arg_.to_f. As of Ruby 1.8,
converting +nil+ generates a +TypeError+.
Float(1) #=3D> 1.0
Float("123.456") #=3D> 123.456
------------------------------------------------------------ String#to_i
str.to_i(base=3D10) =3D> 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 #=3D> 12345
"99 red balloons".to_i #=3D> 99
"0a".to_i #=3D> 0
"0a".to_i(16) #=3D> 10
"hello".to_i #=3D> 0
"1100101".to_i(2) #=3D> 101
"1100101".to_i(8) #=3D> 294977
"1100101".to_i(10) #=3D> 1100101
"1100101".to_i(16) #=3D> 17826049
(note: don't try w ri, you'll be disappointed
then, try testing w irb,
botp@botp-desktop:~$ irb
001:0> Integer ""
ArgumentError: invalid value for Integer: ""
from (irb):1:in `Integer'
from (irb):1
002:0> Integer "1.1"
ArgumentError: invalid value for Integer: "1.1"
from (irb):2:in `Integer'
from (irb):2
irb(main):003:0> Integer "11"
=3D> 11
irb(main):005:0* "".to_i
=3D> 0
irb(main):006:0> "1.1".to_i
=3D> 1
irb(main):007:0> "11".to_i
=3D> 11
irb(main):010:0> "1a23".to_i
=3D> 1
irb(main):011:0> "a123".to_i
=3D> 0
Base on above, Integer() is strict and #to_i is friendly. If you want =
to test if all chars in a string is of literal integer format, then use =
Integer; Integer will give exception otherwise. OTOH, if a string does =
not pass, #to_i will just give you 0 (ie zero).=20
The biggest advantage of #to_i over Integer is that you can change the =
base for conversion.
irb(main):025:0> "ffff".to_i(16)
=3D> 65535
irb(main):026:0> "ffffg".to_i(16)
=3D> 65535
irb(main):027:0> "ffffg".to_i(17)
=3D> 1331116
...but you've probably knew that already..
hth.
kind regards -botp
ps: i think you can handle the Float part