Ruby 1.8.0: Error on "%02d" % "08" or "%02d" % "09"

Hi,

I’m using ruby 1.8.0 (Win native and cygwin version). These two commands

"%02d" % "08"
"%02d" % "09"

result in the error

`%': invalid value for Integer: "08" (ArgumentError)

For other numbers/strings (including “8” and “9”, but also “01”…“07”,
it appears to work as expected – i.e., as I would expect it.

The problem, I want to solve: Turn strings like “1”, “3”, “04”, "03"
into a canonical 2-digit string with zeros in front. Should I use a
different method?

Cheers,
Thomas.

  "%02d" % "08"

it think that you are trying to give it an octal number, try

        "%02d" % "08".to_i

  "%02d" % "09"

Guy Decoux

irb(main):003:0> “%02d” % 08
SyntaxError: compile error
(irb):3: Illegal octal digit
from (irb):3

Leading zeros have a special meaning with integer values.

I’m not really sure of a way around this, especially if the numbers come
from outside of your program.

-austin

···

On Wed, 10 Dec 2003 22:17:06 +0900, Thomas Link wrote:

I’m using ruby 1.8.0 (Win native and cygwin version). These two commands

“%02d” % “08”
“%02d” % “09”

result in the error

`%': invalid value for Integer: “08” (ArgumentError)


austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.12.10
* 09.15.45

`%': invalid value for Integer: “08” (ArgumentError)

irb(main):003:0> “%02d” % 08
SyntaxError: compile error
(irb):3: Illegal octal digit
from (irb):3

Leading zeros have a special meaning with integer values.

Your example is different as the second argument is not a string –
although I understand what you’re hinting at. Anyway, converting the
string into an integer in advance (as proposed by the previous poster)
actually solved the problem. I nevertheless think it’s inconsistent that
string#to_i didn’t expect an octal number too when string#% does so.

Cheers,
Thomas.