Project

General

Profile

Actions

Misc #18150

open

Proposal: Deprecate leading zero syntax to declare octals, since it's extremely confusing (and Python 3 removed it too)

Added by ProGM (Piero Dotti) about 3 years ago. Updated about 3 years ago.

Status:
Open
Assignee:
-
[ruby-core:105142]

Description

Hi there,
I'd like to open a discussion about the leading zeros syntax to declare octal numbers.

Let me give you a little bit of context.

It seems like ruby considers all integers with leading zeros as octal.
For instance, if you write 012, ruby reads it as 10 in decimal.
There is an alternative syntax for this, using an "o" character after the zero, i.e. 0o12

I've discovered this behavior by chance a couple of days ago.
I was declaring a new Date object:

START_DATE = Date.new(2021, 09, 01)

In my mind, I was thinking of ISO 8601 ("2021-09-01") and I wrote it in that way without even thinking about it.

I immediately got a weird error:

SyntaxError ((irb):2: Invalid octal digit)

That was astonishing. "What the heck does this error mean?", I thought.

After a brief research, I've discovered that many languages that come from C have this "weird" behavior.
Javascript, Go, Java, and ruby itself all treat leading zeros like this.
Rust and Elixir, instead, are parsing 00123 like 123, as math notation suggests.
Finally, Python 3+ raises an error.


My proposal is: throw a deprecation warning in ruby 3.x when using this syntax and treat 0011 as 11 from ruby 4+.

Why?

Ruby's claim is "A PROGRAMMER'S BEST FRIEND". As a programmer, I don't consider this behavior very friendly. :(

Let me know what you think about this!

Actions

Also available in: Atom PDF

Like0
Like0Like0