Project

General

Profile

Actions

Bug #20433

open

Hash.inspect for some hash returns syntax invalid representation

Added by tompng (tomoya ishida) 2 months ago. Updated 16 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) 2 months 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) 2 months ago

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

Updated by nobu (Nobuyoshi Nakada) 2 months ago

Ditto for pp.rb.

Updated by jeremyevans0 (Jeremy Evans) about 2 months 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 2 months 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) about 2 months ago

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

+1

Updated by mame (Yusuke Endoh) 24 days ago

Discussed at the dev meeting.

In conclusion, @matz (Yukihiro Matsumoto) wanted to change the return value of Hash#inspect significantly and estimate its compatibility impact:

{ :key => 42 }  #=> {key: 42}     # if the key is a symbol
{ :== => 42 }   #=> {"==": 42}    # if the key is a symbol and quotes are needed
{ "str" => 42 } #=> {"str" => 42} # otherwise (note that `=>` is surrounded by spaces)

# when keys are mixed
{ :== => 1, :key => 2, "str" => 3 } # {"==": 1, key: 2, "str" => 3}

Actually, some solutions were discussed.

Solution 1: insert a space before => only when needed.

{ :key => 1 } # {:key=>1}
{ :== => 1 }  # {:== =>1}
{ :a! => 1 }  # {:a! =>1}

Solution 2: insert spaces before and after => consistently

{ :key => 1 } # {:key => 1}
{ :== => 1 }  # {:== => 1}
{ :a! => 1 }  # {:a! => 1}

Solution 3: quote keys only when needed

{ :== => 1 }  # {:"=="=>1}
{ :a! => 1 }  # {:"a!"=>1}
{ :key => 1 } # {:key=>1}

Matz said he likes Solution 2. However, this has a compatibility issue. It may break some existing test assertions.

Matz had had the idea of denoting symbol keys by a colon in Hash#inspect. And the incompatibility impacts of Solution 2 and of symbol keys by colons are expected to be about the same. Rather than introducing the incompatibilities in two steps, Matz said he wants to change it at once.

Updated by tompng (tomoya ishida) 24 days ago

I created a proof of concept patch for colon style inspect https://github.com/ruby/ruby/pull/10924

Updated by tompng (tomoya ishida) 16 days ago

I investigated the impact to ruby ci.

Need to update these tests. The required change was within the expected range.

bootstraptest
test/coverage
test/error_highlight
test/rdoc
test/reline
test/ruby
test/rubygems
spec/bundler
spec/ruby

Two bundled gem tests failed.

minitest (4 failure)
debug (2 failure)

All of them passes by only changing the expected string/regexp from key=>value to key => value or :key=>value to key: value.

Test against several ruby versions

Test of mspec, default gems and bundled gems needs to pass in several ruby versions.
In most case, changing expected = "message {1=>2}" to expected = "message #{{1=>2}.inspect}" was enough.

There are some case that needs a little more effort like this.

# spec/ruby/core/string/modulo_spec.rb
{ a: obj }.send(@method).should =~ /^\{:a=>#<MockObject:0x[0-9a-f]+>\}$/

# spec/ruby/library/net-http/http/send_request_spec.rb
response.body.should include('"Referer"=>"' + referer + '"')

# minitst/minitest test/minitest/test_minitest_mock.rb
exp = "expected foo(:kw=>false) => nil, got [foo(:kw=>true) => nil]"

@matz (Yukihiro Matsumoto) For your information

Actions

Also available in: Atom PDF

Like0
Like1Like0Like0Like0Like0Like0Like0Like0Like0