?
Solved

Ruby - sprintf invalid value for interger

Posted on 2008-06-26
4
Medium Priority
?
672 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to count occurrences of each item in an array.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

762 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