Solved

Ruby - sprintf invalid value for interger

Posted on 2008-06-26
4
666 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 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

724 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