Project

General

Profile

Actions

Bug #20433

open

Hash.inspect for some hash returns syntax invalid representation

Added by tompng (tomoya ishida) about 1 month ago. Updated 10 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:117564]

Description

For these hashes, Hash.inspect returns a syntax invalid representation:

{ :a! => 1 } # {:a!=>1}
{ :a? => 1 } # {:a?=>1}
{ :* => 1 }  # {:*=>1}
{ :== => 1 } # {:===>1}
{ :< => 1 }  # {:<=>1}

eval(hash.inspect) will raise SyntaxError.
Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases.

Maybe related to https://bugs.ruby-lang.org/issues/20235

Updated by byroot (Jean Boussier) about 1 month ago

An easy fix would be for Hash#inspect to include spaces around =>. It would also read much nicer in my opinion.

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

Or use {a!: 1} for symbol keys?

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

Ditto for pp.rb.

Updated by jeremyevans0 (Jeremy Evans) 11 days ago

I submitted a pull request for the most backwards compatible change, which only uses spaces around => for symbols ending in [!?*=<]: https://github.com/ruby/ruby/pull/10737

Updated by Eregon (Benoit Daloze) 11 days ago

I think always having spaces would help readability.
If we don't want to change that, then how about simply quoting these symbols that need it?

Updated by Dan0042 (Daniel DeLorme) 10 days ago

If we don't want to change that, then how about simply quoting these symbols that need it?

+1

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0