The Day I Learned 08 Is Not Eight in Ruby
It all started innocently enough, i was in Pry, trying to create a date:
Date.new(2018, 08, 01)I hit enter, expecting a nice #<Date: 2018-08-01 …>.
Instead, Ruby slapped me with:
(eval):2: Invalid octal digitWait — octal? I’m just trying to make a date!
Step 1: The Confusion
Like most people, I read the error twice. Then I looked at my code.
Maybe I had a typo? Maybe Pry was haunted? Maybe it didn’t like far-future dates?
I tried:
Date.new(2018, 8, 1)…and it worked.
That’s when the mystery deepened: why does 8 work, but 08 doesn’t?
Step 2: Searching for the culprit
I copied the error message into Google:
ruby invalid octal digit
Within minutes, I found a Stack Overflow thread explaining that Ruby interprets numbers with a leading zero as octal (base-8).
In octal, valid digits are only 0–7.
The moment Ruby saw my 08, it screamed: “You can’t have an 8 in base-8!”
Step 3: The “aha!” moment
It wasn’t a date problem, it wasn’t Pry, t wasn’t the year 2018 being cursed.
Get Samy’s stories in your inbox
Join Medium for free to get updates from this writer.
It was me, trying to zero-pad a numeric literal like it was a string. Ruby happily allowed 01 (octal 1), but 08 broke the rules.
Step 4: The fix
Once I knew what was going on, the fix was simple: don’t zero-pad numbers in code.
Date.new(2018, 8, 1) # WorksIf I need padding (e.g., "2018-08-01"), that belongs in string formatting, not numeric literals:
year, month, day = 2018, 8, 1
("%04d-%02d-%02d" % [year, month, day])
# => "2018-08-01"Step 5: My solution checklist
Here’s how I solved it and how you can avoid falling into the same trap:
- Remove leading zeros from numeric literals — just type
8instead of08. - If you need padding, use strings and formatting.
- Prefer parsing from ISO-8601 strings when hard-coding dates:
require "date"
Date.iso8601("2018-08-01")2. Teach your team — this is a subtle Ruby quirk that’s bitten many devs.
Advice going forward
- Treat numbers in code as pure values — no formatting, no padding.
- Remember that in Ruby, a leading zero means octal (unless you explicitly use
0ofor octal literals). - For human-friendly date literals, stick to strings.
- Lint early, lint often — tools like RuboCop catch this before it hits runtime.
That one little 0 cost me 15 minutes of confusion and a trip down Ruby’s historical quirks. But now, when I see (eval): Invalid octal digit, I know exactly what’s wrong.
And I’ll never write 08 in Ruby again.








