From: Gavin Sinclair Date: 2002-08-04T21:58:21+09:00 Subject: #Re: String -> Integer anomoly? Hmmm, strikes me as a violation of POLS. Integer("012") != "012".to_i What do others think? It's a shame you can't see documentation for Integer() with 'ri' (a tool I love dearly!), or have I missed it? It would be appropriate to be able to specify the radix when converting using Integer, and also to tell it to guess. It would also be nice to provide a default value in case of failure. Gavin ----- Original Message ----- From: "YANAGAWA Kazuhisa" To: "ruby-talk ML" Sent: Sunday, August 04, 2002 1:20 PM Subject: Re: String -> Integer anomoly? > In message <021901c23b52$c7d86580$0300a8c0@austin.rr.com> > hal9000@hypermetrics.com writes: > > > > Why does "09".to_i return 9 > > > and Integer("09") raise an error? > (snip) > > "09".to_i # 9 (assumes decimal with leading 0) > > "0xabc".to_i # 0 > > And then Integer() considers "09" as a representation of an octal > number, but it contains "9", exceeding radix of octal. > > Integer("12") #=> 12 in decimal > Integer("012") #=> 10 in decimal > Integer("0x12") #=> 18 in decimal > > > Integer() raises exception on ill-formed string passed for its > argument, where ill-formed means whole expression doesn't satisfy > conditions for integer literals. > > > ruby -e 'p 012' #=> 10 > > ruby -e 'p 09' #=> -e:1: Illegal octal digit > > > # Hmm, Integer() should show more descriptive message like on syntax > # checking? > > > -- > kjana@dm4lab.to August 4, 2002 > What is done can't be undone.