[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-core
Subject:    [ruby-core:98950] [Ruby master Feature#16985] Improve `pp` for `Hash` and `String`
From:       shevegen () gmail ! com
Date:       2020-06-26 9:51:53
Message-ID: redmine.journal-86326.20200626095152.182 () ruby-lang ! org
[Download RAW message or body]

Issue #16985 has been updated by shevegen (Robert A. Heiler).


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.

----------------------------------------
Feature #16985: Improve `pp` for `Hash` and `String`
https://bugs.ruby-lang.org/issues/16985#change-86326

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: akr (Akira Tanaka)
----------------------------------------
Could we improve `pp` for `Hash` and `String`:

```ruby
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?)



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic