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 2 months ago. Updated about 2 months ago.

Status:
Open
Priority:
Normal
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!

Updated by mame (Yusuke Endoh) about 2 months ago

The syntax is primarily used for file permissions. By grepping the source code of all public gems, you see thousands of lines like :mode => 0644.

$ gem-codesearch -f \.rb "\b0644\b" | wc -l
8324

$ gem-codesearch -f \.rb "\b0755\b" | wc -l
5277

$ gem-codesearch -f \.rb "\b0o644\b" | wc -l
142

$ gem-codesearch -f \.rb "\b0o755\b" | wc -l
161

I understand your feelings, but the syntax is very trivial. IMO, this deprecation looks not worth killing so many existing assets.

Updated by znz (Kazuhiro NISHIYAMA) about 2 months ago

I think supporting 08 and 09 only satisfy such usecases.

Actions

Also available in: Atom PDF