Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse a number from a String that may have a leading zero?

In ruby I am parsing a date in the following format: 24092008. I want to convert each section (year, month, date) into a number.

I have split them up using a regex which produces three Strings which I am passing into the Integer constructor.

  date =~ /^([\d]{2})([\d]{2})([\d]{4})/
  year = Integer($3)
  month = Integer($2)
  day = Integer($1)

When it hits the month line it crashes as follows:

`Integer': invalid value for Integer: "09" (ArgumentError)

It took me a while to realise that it's interpreting the leading zero as Octal and 09 is not a valid Octal number (it works fine with "07").

Is there an elegant solution to this or should I just test for numbers less than 10 and remove the zero first?

Thanks.

like image 610
Darren Greaves Avatar asked Sep 28 '08 20:09

Darren Greaves


1 Answers

I'm not familiar with regexes, so forgive me if this answer's off-base. I've been assuming that $3, $2, and $1 are strings. Here's what I did in IRB to replicate the problem:

irb(main):003:0> Integer("04")
=> 4
irb(main):004:0> Integer("09")
ArgumentError: invalid value for Integer: "09"
    from (irb):4:in `Integer'
    from (irb):4
    from :0

But it looks like .to_i doesn't have the same issues:

irb(main):005:0> "04".to_i
=> 4
irb(main):006:0> "09".to_i
=> 9
like image 90
Atiaxi Avatar answered Sep 21 '22 23:09

Atiaxi