Bug #20433
openHash.inspect for some hash returns syntax invalid representation
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) 20 days 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) 20 days ago
Or use {a!: 1}
for symbol keys?
Updated by nobu (Nobuyoshi Nakada) 20 days ago
Ditto for pp.rb.
Updated by jeremyevans0 (Jeremy Evans) about 9 hours 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) about 7 hours 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?