[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Ruby - sprintf invalid value for interger

Posted on 2008-06-26
4
Medium Priority
?
679 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

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…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

649 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