B
Ben Gribaudo
Hello,
I am puzzled by some strange (to me) behavior exhibited by String's %
method as well as sprintf. Both "%02d" % "07" and sprintf("%02d","07")
output "07". However, "%02d" % "08" and sprintf("%02d","08") both raise
'ArgumentError: invalid value for Integer: "08"'. Why does a string of
"07" work while "08" does not?
This is true when I test this on a MS Windows XP Pro box running Ruby
1.6.8 and when I test it on a Debian Woody box (kernal 2.4.19) running
Ruby 1.8.2. IRB examples and results are below.
Thank you,
Ben
Win XP box
-------------
irb(main):014:0> "%02d" % "07"
=> "07"
irb(main):015:0> "%02d" % "08"
ArgumentError: invalid value for Integer: "08"
from (irb):15:in `%'
from (irb):15
irb(main):017:0> sprintf("%02d","07")
=> "07"
irb(main):018:0> sprintf("%02d","08")
ArgumentError: invalid value for Integer: "08"
from (irb):18:in `sprintf'
from (irb):18
Linux box
----------
irb(main):003:0> "%02d" % "07"
=> "07"
irb(main):004:0> "%02d" % "08"
ArgumentError: invalid value for Integer: "08"
from (irb):4:in `%'
from (irb):4
irb(main):005:0> sprintf("%02d","07")
=> "07"
irb(main):006:0> sprintf("%02d","08")
ArgumentError: invalid value for Integer: "08"
from (irb):6:in `sprintf'
from (irb):6
I am puzzled by some strange (to me) behavior exhibited by String's %
method as well as sprintf. Both "%02d" % "07" and sprintf("%02d","07")
output "07". However, "%02d" % "08" and sprintf("%02d","08") both raise
'ArgumentError: invalid value for Integer: "08"'. Why does a string of
"07" work while "08" does not?
This is true when I test this on a MS Windows XP Pro box running Ruby
1.6.8 and when I test it on a Debian Woody box (kernal 2.4.19) running
Ruby 1.8.2. IRB examples and results are below.
Thank you,
Ben
Win XP box
-------------
irb(main):014:0> "%02d" % "07"
=> "07"
irb(main):015:0> "%02d" % "08"
ArgumentError: invalid value for Integer: "08"
from (irb):15:in `%'
from (irb):15
irb(main):017:0> sprintf("%02d","07")
=> "07"
irb(main):018:0> sprintf("%02d","08")
ArgumentError: invalid value for Integer: "08"
from (irb):18:in `sprintf'
from (irb):18
Linux box
----------
irb(main):003:0> "%02d" % "07"
=> "07"
irb(main):004:0> "%02d" % "08"
ArgumentError: invalid value for Integer: "08"
from (irb):4:in `%'
from (irb):4
irb(main):005:0> sprintf("%02d","07")
=> "07"
irb(main):006:0> sprintf("%02d","08")
ArgumentError: invalid value for Integer: "08"
from (irb):6:in `sprintf'
from (irb):6