Saturday, December 3, 2011

Getting error "Illegal octal digit"

While trying to execute below case method to display month, getting 'Illegal octal digit' for August and September month only:

def self.convert_month(month)
    case month
      when "Jan" :         01
      when "Feb" :        02
      when "Mar" :        03
      when "Apr" :        04
      when "May" :       05
      when "Jun" :         06
      when "Jul" :          07
      when "Aug" :       08
      when "Sep" :        09
      when "Oct" :        10
      when "Nov" :       11
      when "Dec" :        
  end

Solution

Ruby is interpreting numbers that have a leading 0 as being in octal (base 8). Thus the digits 8 and 9 are not valid. So,  I removed leading zero from Aug and Sep month and changed the method as:

when "Aug" :        8
when "Sep" :         9

No comments:

Post a Comment