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?
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.