https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112020-06-25T22:04:20ZRuby Issue Tracking SystemRuby master - Feature #16985: Improve `pp` for `Hash` and `String`https://bugs.ruby-lang.org/issues/16985?journal_id=863162020-06-25T22:04:20ZEregon (Benoit Daloze)
<ul></ul><p>I think <code>symbol_key: value</code> would be nice in Hash#inspect, even if there are also non-Symbol keys.</p>
<p>I think changing String#inspect might be confusing, I'm used to see Strings always starting with a <code>"</code> for <code>p string</code>.</p> Ruby master - Feature #16985: Improve `pp` for `Hash` and `String`https://bugs.ruby-lang.org/issues/16985?journal_id=863172020-06-25T22:04:44ZEregon (Benoit Daloze)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/86317/diff?detail_id=57404">diff</a>)</li></ul> Ruby master - Feature #16985: Improve `pp` for `Hash` and `String`https://bugs.ruby-lang.org/issues/16985?journal_id=863192020-06-26T01:44:11Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>For <code>String</code>, I meant to say that current format would be kept the same except if the single quote version exist and has no escape.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">pp</span> <span class="s2">"Hello"</span> <span class="c1"># => "Hello" (no change)</span>
<span class="n">pp</span> <span class="s2">"Won't</span><span class="se">\n</span><span class="s2">work </span><span class="se">\"</span><span class="s2">Marc-André</span><span class="se">\"</span><span class="s2">"</span> <span class="c1"># => "Won't\nwork \"Marc-André\"" (no change)</span>
<span class="n">pp</span> <span class="s2">"Hello </span><span class="se">\"</span><span class="s2">Marc-André</span><span class="se">\"</span><span class="s2">"</span> <span class="c1"># => 'Hello "Marc-André"' (simpler version)</span>
</code></pre> Ruby master - Feature #16985: Improve `pp` for `Hash` and `String`https://bugs.ruby-lang.org/issues/16985?journal_id=863202020-06-26T02:01:23Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>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.</p> Ruby master - Feature #16985: Improve `pp` for `Hash` and `String`https://bugs.ruby-lang.org/issues/16985?journal_id=863222020-06-26T06:01:05Zsawa (Tsuyoshi Sawada)
<ul></ul><p>shyouhei (Shyouhei Urabe) wrote in <a href="#note-4">#note-4</a>:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>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?</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="c1"># => {'3': 1}</span>
<span class="c1"># => {"3": 1}</span>
</code></pre>
<p>And as for me, I am rather more against the proposal here regarding hash than I am with string.</p> Ruby master - Feature #16985: Improve `pp` for `Hash` and `String`https://bugs.ruby-lang.org/issues/16985?journal_id=863242020-06-26T07:58:50Zvo.x (Vit Ondruch)v.ondruch@tiscali.cz
<ul></ul><p>I wish the hashrocket stays and the syntax is consistent without exceptions for symbol.</p> Ruby master - Feature #16985: Improve `pp` for `Hash` and `String`https://bugs.ruby-lang.org/issues/16985?journal_id=863262020-06-26T09:51:52Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I don't have any big preference, but I tend to agree with sawa too.</p>
<p>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.</p>
<p>(1) Display "foo: bar" rather than ":foo => bar".</p>
<p>and</p>
<p>(2) Using:</p>
<pre><code>'My name is "abc"'
</code></pre>
<p>Rather than:</p>
<pre><code>"My name is \"Marc-André\""
</code></pre>
<p>Sorry if I misunderstood it initially. I believe that both situations are fine BUT!<br>
I'll soon add what I think may be a problem.</p>
<p>First, let me say that I somewhat agree with vo.x in the sense that the hash rocket<br>
is the "default" syntax for hash, and the foo: :bar is a "shortcut". Note that I use<br>
the foo: :bar notation a lot myself, but ruby even sort of shows you that it is<br>
an alias to the hashrocket rather than the reverse - in IRB:</p>
<pre><code>hash = { foo: :bar, cat: :tom }
hash # => {:foo=>:bar, :cat=>:tom}
</code></pre>
<p>So there we have the => notation. And I think this should stay the default too,<br>
similar to what vo.x wrote.</p>
<p>At the same time, though, I think a part of marcandre's suggestion was to have<br>
pp be more flexible in use. So if a ruby user prefers what marcandre suggested,<br>
then I am fine with this. I believe that this should not become the default,<br>
though. The current default should remain, but if people are able to customize<br>
it to their likings, then I am all for it.</p>
<p>How to customize this? Well. Perhaps PP could have a toplevel configuration<br>
style or something; and an option hash for pp() itself. (Toplevel configuration<br>
could then be used to set up PP once.)</p>
<p>I should also say that I know the ap (awesome_print), and it has colour support,<br>
but I realized that the colours confused me more than it helped me, so I went<br>
back to pp. I use pp a LOT. I don't have a really strong opinion per se, but<br>
I think it would be better to default to how things are right now, but allow<br>
people to customize the behaviour of pp (perhaps even at compile time if they<br>
feel strongly about it).</p>
<p>benoit wrote:</p>
<blockquote>
<p>I think symbol_key: value would be nice in Hash#inspect, even if there are<br>
also non-Symbol keys.</p>
</blockquote>
<p>Well, if people can decide this on their own, then that is fine - but I personally<br>
would rather retain the current behaviour here, for my own projects.</p>