From: Michael W. Ryder [mailto:_mwryder / worldnet.att.net] # I am trying to figure out why methods that seem to do the same thing # behave so differently. For example Integer("0377") returns 255 as # expected for an octal number but "0377".to_i returns 377. Or why # Integer("12e3") crashes while Float("12e3") returns 12000.0 # as expected. # I was hoping to find somewhere that tells when to use one # or the other # method depending upon expected inputs. It is very time consuming to # 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) => 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) #=> 123 Integer("0x1a") #=> 26 Integer(Time.new) #=> 1049896590 ----------------------------------------------------------- Kernel#Float Float(arg) => 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) #=> 1.0 Float("123.456") #=> 123.456 ------------------------------------------------------------ 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 (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" => 11 irb(main):005:0* "".to_i => 0 irb(main):006:0> "1.1".to_i => 1 irb(main):007:0> "11".to_i => 11 irb(main):010:0> "1a23".to_i => 1 irb(main):011:0> "a123".to_i => 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). 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) => 65535 irb(main):026:0> "ffffg".to_i(16) => 65535 irb(main):027:0> "ffffg".to_i(17) => 1331116 ...but you've probably knew that already.. hth. kind regards -botp ps: i think you can handle the Float part ;)