Solved

Ruby - sprintf invalid value for interger

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now