https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112013-06-13T09:54:23ZRuby Issue Tracking SystemRuby master - Feature #8520: Distinct to_s methods for Array, Hash...https://bugs.ruby-lang.org/issues/8520?journal_id=399002013-06-13T09:54:23Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li><li><strong>Priority</strong> changed from <i>Normal</i> to <i>3</i></li></ul><p>Ruby 2.0 uses #to_s as an alias to #inspect, so redefine #inspect instead of #to_s for your class.<br>
This might be controversial, so I don't close this issue.</p>
<p>Matz.</p> Ruby master - Feature #8520: Distinct to_s methods for Array, Hash...https://bugs.ruby-lang.org/issues/8520?journal_id=399012013-06-13T13:10:25ZAnonymous
<ul></ul><p>According to my feelings, in your example, class A shirks its duty of defining<br>
sensible #inspect, and the ugly ouput is a reminder of that. I feel that 2.0<br>
behavior is more correct, in the sense that the concerns of string conversion vs.<br>
user inspection are in fact orthogonal.</p> Ruby master - Feature #8520: Distinct to_s methods for Array, Hash...https://bugs.ruby-lang.org/issues/8520?journal_id=399102013-06-13T23:25:48Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>It could be a good idea to specialize <code>to_s</code> for structures.</p>
<p>What would be the downside of having Array#to_s call <code>to_s</code> on its elements while Array#inspect would call <code>inspect</code>? This would be the same idea as Array#eql? comparing with <code>eql?</code> while Array#== uses <code>==</code>, even though for most classes they are aliases</p> Ruby master - Feature #8520: Distinct to_s methods for Array, Hash...https://bugs.ruby-lang.org/issues/8520?journal_id=399132013-06-14T00:37:24ZLFDM (Gernot Höflechner)
<ul></ul><p>Thanks for the responses guys!</p>
<p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/13">@matz (Yukihiro Matsumoto)</a> and boris:<br>
I deliberately left that out in my first message, when I probably shouldn't: Of course the issue can be overcome quite easily: as you said, just redefine inspect instead of to_s or alias it - that's just what I am doing in the real world.<br>
This has imo various downsides though:</p>
<p>a) - We're somehow back to ruby 1.9 behaviour, as you can't call normal inspect anymore, when you - for whatever reason - wan't to see the whole output. That too can be overcome, catching the old inspect method with something else and so on... but that might be a little too much hassle for something as simple as that.<br>
b) - It's probably semantically not ideal: Let's imagine a poll, where relatively new rubyist are asked the following question: When you call #to_s on an array, what method gets called on all its elements? And what message gets sent when you call Array#inspect? I am quite confident that the result would be lopsided: to_s passes to_s, inspect passes inspect. I am not even sure if you have to limit this poll to new rubyist, I guess even experienced programmers might fall for this "trap".</p>
<p>I am with Marc-Andre, I cannot see a downside in having two distinct approaches for Array/Hash#to_s and #inspect.</p>
<p>Still I can understand the point Boris made: Going back to my dumbed down example a case could be made that #inspect was the method I should have been looking for in the first place: Not a string conversion, but an inspection of an object for debugging reasons. That's almost a philosophical debate. The way I see it, #to_s gives me a structured output of something in an easily digestible format - which I may like for debugging f.e. - while #inspect gives me raw and as detailed as possible information about my data.<br>
But I think it doesn't matter where you stand here: Just let the user decide if he wants to use #to_s or #inspect - and give him just that.</p> Ruby master - Feature #8520: Distinct to_s methods for Array, Hash...https://bugs.ruby-lang.org/issues/8520?journal_id=1050922023-10-26T09:41:58Zp8 (Petrik de Heus)
<ul></ul><p>There is an open issue in Rails to limit <code>ActiveRecord::Base#inspect</code> for performance reasons.<br>
<a href="https://github.com/rails/rails/issues/49707" class="external">https://github.com/rails/rails/issues/49707</a></p>
<p>Calling <code>to_s</code> on a <code>Hash</code> will call <code>inspect</code> on its contents.<br>
<code>ActiveRecord::Base#inspect</code> prints all the records attributes. It loops through all attributes and filters sensitive ones.<br>
So calling <code>to_s</code> on a <code>Hash</code> with a lot of ActiveRecord instances/attributes can result in performance issues, as it filters all attributes.</p>
<p>Of course this issue can be fixed in Rails by changing <code>ActiveRecord::Base#inspect</code>, or not calling to <code>to_s</code> on a large <code>Hash</code>.<br>
But it might not be obvious to everyone that <code>Hash#to_s</code> is an alias to <code>Hash#inspect</code>, as a lot of objects have different behaviour for <code>to_s</code> and <code>inspect</code>.</p> Ruby master - Feature #8520: Distinct to_s methods for Array, Hash...https://bugs.ruby-lang.org/issues/8520?journal_id=1050932023-10-26T10:56:13ZHanmac (Hans Mackowiak)hanmac@gmx.de
<ul></ul><p>p8 (Petrik de Heus) wrote in <a href="#note-5">#note-5</a>:</p>
<blockquote>
<p>There is an open issue in Rails to limit <code>ActiveRecord::Base#inspect</code> for performance reasons.<br>
<a href="https://github.com/rails/rails/issues/49707" class="external">https://github.com/rails/rails/issues/49707</a></p>
</blockquote>
<p>That sounds more like a Rails problem than a Ruby Problem</p>
<p>if the objects in an Array or Hash aren't in their already fully loaded form (with no extra DB calls required),<br>
then its more of a problem of the library using it.</p>