# String -> Integer anomoly?

Why does “09”.to_i return 9
and Integer(“09”) raise an error?

···

Gavin Sinclair Software Engineer
Sydney, Australia Soyabean Software Pty Ltd

Arguably a feature. My understanding is that
this was done on purpose.

It sometimes gets me confused, though.

I believe that Integer is “smarter” than
String#to_i.

This came up when we were discussing the fact
that to_i doesn’t raise exceptions.

“abc”.to_i # 0

It also doesn’t know different bases.

“09”.to_i # 9 (assumes decimal with leading 0)
“0xabc”.to_i # 0

Someone else can elaborate more.

Hal

···

----- Original Message -----
From: “Gavin Sinclair” gsinclair@soyabean.com.au
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, August 03, 2002 7:39 PM
Subject: String → Integer anomoly?

Why does “09”.to_i return 9
and Integer(“09”) raise an error?

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
``````

# checking?

···

In message 021901c23b52\$c7d86580\$0300a8c0@austin.rr.com hal9000@hypermetrics.com writes:

kjana@dm4lab.to August 4, 2002
What is done can’t be undone.

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” kjana@dm4lab.to
To: “ruby-talk ML” ruby-talk@ruby-lang.org
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
``````

# checking?

kjana@dm4lab.to August 4, 2002
What is done can’t be undone.

It's a shame you can't see documentation for Integer() with 'ri' (a tool I

ri Kernel#Integer

Guy Decoux

Hi,

···

At Sun, 4 Aug 2002 21:58:21 +0900, Gavin Sinclair wrote:

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.

Integer() isn’t String specific. String#to_i in 1.7 takes

Gavin Sinclair wrote:

Hmmm, strikes me as a violation of POLS.

Integer(“012”) != “012”.to_i

What do others think?

Doesn’t violate my PoLS atleast. Since Integer is a method of Kernel,
and to_i (in this case) is a method of String, I am not surprised to see
them have different takes on the matter. As in
“Hey, I’m the kernel, and I care about hex, octal and such geeky
stuff.”
and
“Hey, I’m a (l)user input string, you don’t think I’d be trying to be
octal or anything that geeky, do ya?”

···

([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 4. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

Interesting, because it surprised me when I found out that Integer was a
Kernel method! I was thinking it must be a method of Integer, and playing
with irb, trying to create a class that could be instantiated in that way
(like Dir[“.”]). Alas, I couldn’t and it was not until I saw Guy’s message

Now, if Kernel::Integer() is so geeky, why can’t it accept a radix?

Gavin

···

----- Original Message -----
From: “Kent Dahl” kentda@stud.ntnu.no

Gavin Sinclair wrote:

Hmmm, strikes me as a violation of POLS.

Integer(“012”) != “012”.to_i

What do others think?

Doesn’t violate my PoLS atleast. Since Integer is a method of Kernel,
and to_i (in this case) is a method of String, I am not surprised to see
them have different takes on the matter. As in
“Hey, I’m the kernel, and I care about hex, octal and such geeky
stuff.”
and
“Hey, I’m a (l)user input string, you don’t think I’d be trying to be
octal or anything that geeky, do ya?”

Ahh, I see, because it’s not String-specific (thanks Nobu). Fair enough.

Gavin

···

----- Original Message -----
From: “Gavin Sinclair” gsinclair@soyabean.com.au

Now, if Kernel::Integer() is so geeky, why can’t it accept a radix?