Solved

Ruby - sprintf invalid value for interger

Posted on 2008-06-26
4
647 Views
Last Modified: 2012-05-05
I'll start by saying I did not write  the Ruby script and know nothing about Ruby.  I inherited this and noticed that when it ran it was producing the following error:

myscript.rb:105:in `sprintf': invalid value for Integer: "08" (ArgumentError)

From what I can tell, it looks like on line 105 the code snippet is trying to convert a date format of  6/26/08 to 2008-06-26.  My guess is that the 08 is being looked at as if it is a Octal value and if the year was 07 it would work fine?  Of course I could be totally wrong.  Anyway, can anyone suggest a fix?   Did I provide enough info?
 


data.scan(/^(\d+)\/(\d+)\/(\d+)/) { |m,d,y| dateStamp = sprintf "20%02d-%02d-%02d", y, m, d }

Open in new window

0
Comment
Question by:credog
  • 2
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 250 total points
ID: 21876237
8 would be a valid integer
08 would not be a valid integer
so you have to change your pattern to get rid of leading zeroes
try
data.scan(/^0*(\d+)\/0*(\d+)\/0*(\d+)/) { |m,d,y| dateStamp = sprintf "20%02d-%02d-%02d", y, m, d }
0
 

Author Comment

by:credog
ID: 21876598
Gertone,

That seemed to fix the problem.  

I am assuming that this fix will only strip the leading zeros and will not have any effect on any other values that the date may be?  Since the zeros are added back on with sprintf all should basically work as before?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 21877064
the fix will indeed only strip the leading zeroes (preventing what you require to be an integer to be invalid)
and indeed these zeroes are added back in later

by the way, this would work as well
data.scan(/^(\d+)\/(\d+)\/(\d+)/) { |m,d,y| dateStamp = sprintf "20%02d-%02d-%02d", y.to_i, m.to_i, d.to_i}
0
 

Author Closing Comment

by:credog
ID: 31471028
Quick, timely and accurate solution.  Expert provided  information that helped in understanding the solution and made it easy to implement..
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to dynamically set the form action using jQuery.

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question