← Back to list

The Day I Learned 08 Is Not Eight in Ruby

It all started innocently enough, i was in Pry, trying to create a date:

Samy · 2025-08-09 19:07 · 2 claps · 1.9 min read
#ruby #octal #date #debugging
Open on Medium ↗
Wiki topics: 💻 · Programming

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 digit

Wait — 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 07. 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.

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)  # Works

If 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:

  1. Remove leading zeros from numeric literals — just type 8 instead of 08.
  2. If you need padding, use strings and formatting.
  3. 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 0o for 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.


메타데이터
post_id
d3da155e57fe
slug
the-day-i-learned-08-is-not-eight-in-ruby-d3da155e57fe
url
https://medium.com/@samidghim/the-day-i-learned-08-is-not-eight-in-ruby-d3da155e57fe
canonical_url
https://medium.com/@samidghim/the-day-i-learned-08-is-not-eight-in-ruby-d3da155e57fe
author_url
https://medium.com/@samidghim
status
ok
fetched_at
2026-07-18 09:32:48