Project

General

Profile

Feature #16985

Improve `pp` for `Hash` and `String`

Added by marcandre (Marc-Andre Lafortune) about 1 month ago. Updated about 1 month ago.

Status:
Open
Priority:
Normal
Target version:
-
[ruby-core:98938]

Description

Could we improve pp for Hash and String:

pp({hello: 'My name is "Marc-André"'})
# =>
{hello: 'My name is "Marc-André"'} 
# instead of
{:hello=>"My name is \"Marc-André\""}

If any key is non-symbol, they would continue to be output as <key> => <value>. If a string contains single quotes, or characters that need escaping (e.g. "\n"), current format would be used.

I'll gladly provide a PR if this is deemed acceptable.

I would even like this for String#inspect and Hash#inspect but it's not clear if this could lead to much incompatibility (maybe test suites?)

Updated by Eregon (Benoit Daloze) about 1 month ago

I think symbol_key: value would be nice in Hash#inspect, even if there are also non-Symbol keys.

I think changing String#inspect might be confusing, I'm used to see Strings always starting with a " for p string.

#2

Updated by Eregon (Benoit Daloze) about 1 month ago

  • Description updated (diff)

Updated by marcandre (Marc-Andre Lafortune) about 1 month ago

For String, I meant to say that current format would be kept the same except if the single quote version exist and has no escape.

pp "Hello" # => "Hello" (no change)
pp "Won't\nwork \"Marc-André\"" # => "Won't\nwork \"Marc-André\"" (no change)
pp "Hello \"Marc-André\"" # => 'Hello "Marc-André"' (simpler version)

Updated by shyouhei (Shyouhei Urabe) about 1 month ago

There are lots of debatable points on String#inspect (for instance its return value depends on runtime locale, why?). Improving it can quite easily jump into a bikeshed. You might want to separate Hash's and String's discussions and focus on the Hash side, because almost nobody would against that part I guess.

Updated by sawa (Tsuyoshi Sawada) about 1 month ago

shyouhei (Shyouhei Urabe) wrote in #note-4:

There are lots of debatable points on String#inspect (for instance its return value depends on runtime locale, why?). Improving it can quite easily jump into a bikeshed. You might want to separate Hash's and String's discussions and focus on the Hash side, because almost nobody would against that part I guess.

The issue on hash and that on string can be discussed separately, but actually, they are related. If a symbol key needs quotation, should that be single quotes or double quotes?

# => {'3': 1}
# => {"3": 1}

And as for me, I am rather more against the proposal here regarding hash than I am with string.

Updated by vo.x (Vit Ondruch) about 1 month ago

I wish the hashrocket stays and the syntax is consistent without exceptions for symbol.

Updated by shevegen (Robert A. Heiler) about 1 month ago

I don't have any big preference, but I tend to agree with sawa too.

When I first read the proposal, I was confused in that two suggestions seem to be combined - or at the least I was reading it that way.

(1) Display "foo: bar" rather than ":foo => bar".

and

(2) Using:

'My name is "abc"'

Rather than:

"My name is \"Marc-André\""

Sorry if I misunderstood it initially. I believe that both situations are fine BUT!
I'll soon add what I think may be a problem.

First, let me say that I somewhat agree with vo.x in the sense that the hash rocket
is the "default" syntax for hash, and the foo: :bar is a "shortcut". Note that I use
the foo: :bar notation a lot myself, but ruby even sort of shows you that it is
an alias to the hashrocket rather than the reverse - in IRB:

hash = { foo: :bar, cat: :tom }
hash # => {:foo=>:bar, :cat=>:tom}

So there we have the => notation. And I think this should stay the default too,
similar to what vo.x wrote.

At the same time, though, I think a part of marcandre's suggestion was to have
pp be more flexible in use. So if a ruby user prefers what marcandre suggested,
then I am fine with this. I believe that this should not become the default,
though. The current default should remain, but if people are able to customize
it to their likings, then I am all for it.

How to customize this? Well. Perhaps PP could have a toplevel configuration
style or something; and an option hash for pp() itself. (Toplevel configuration
could then be used to set up PP once.)

I should also say that I know the ap (awesome_print), and it has colour support,
but I realized that the colours confused me more than it helped me, so I went
back to pp. I use pp a LOT. I don't have a really strong opinion per se, but
I think it would be better to default to how things are right now, but allow
people to customize the behaviour of pp (perhaps even at compile time if they
feel strongly about it).

benoit wrote:

I think symbol_key: value would be nice in Hash#inspect, even if there are
also non-Symbol keys.

Well, if people can decide this on their own, then that is fine - but I personally
would rather retain the current behaviour here, for my own projects.

Also available in: Atom PDF