https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17097754782011-12-14T10:05:53ZRuby Issue Tracking SystemRuby master - Bug #5759: flatten calls to_ary on everythinghttps://redmine.ruby-lang.org/issues/5759?journal_id=227972011-12-14T10:05:53Zdrbrain (Eric Hodel)drbrain@segment7.net
<ul></ul><p>Use Kernel#Array:</p>
<p>$ ruby -e 'p Array("a\nb"), Array(["a\nb"])'<br>
["a\nb"]<br>
["a\nb"]</p> Ruby master - Bug #5759: flatten calls to_ary on everythinghttps://redmine.ruby-lang.org/issues/5759?journal_id=228152011-12-14T19:23:09Zregularfry (Alex Young)alex@blackkettle.org
<ul></ul><p>On 14/12/11 01:05, Eric Hodel wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-1 status-6 priority-4 priority-default closed" title="Bug: flatten calls to_ary on everything (Rejected)" href="https://redmine.ruby-lang.org/issues/5759">#5759</a> has been updated by Eric Hodel.</p>
<p>Use Kernel#Array:</p>
<p>$ ruby -e 'p Array("a\nb"), Array(["a\nb"])'<br>
["a\nb"]<br>
["a\nb"]</p>
</blockquote>
<p>Or a splat:</p>
<p>ruby-1.9.3-p0 :001 > a="a\nb"; [*a]<br>
=> ["a\nb"]<br>
ruby-1.9.3-p0 :002 > a=["a\nb"]; [*a]<br>
=> ["a\nb"]</p>
<p>Not sure I disagree that #flatten should check first (or just leave the<br>
exception uncaught) though.</p>
<p>--<br>
Alex</p>
<blockquote>
<hr>
<p>Bug <a class="issue tracker-1 status-6 priority-4 priority-default closed" title="Bug: flatten calls to_ary on everything (Rejected)" href="https://redmine.ruby-lang.org/issues/5759">#5759</a>: flatten calls to_ary on everything<br>
<a href="http://redmine.ruby-lang.org/issues/5759" class="external">http://redmine.ruby-lang.org/issues/5759</a></p>
<p>Author: Thomas Sawyer<br>
Status: Open<br>
Priority: Normal<br>
Assignee:<br>
Category:<br>
Target version: 1.9.3<br>
ruby -v: ruby 1.9.3dev (2011-09-23 revision 33323) [x86_64-linux]</p>
<p>I often ensure that I have an array by doing:</p>
<p>def foo=(x)<br>
@foo = [x].flatten<br>
end</p>
<p>But this has turned into a problem as of late, as it seems #flatten is calling #to_ary on every element in the array, and apparently catching the error raised if #to_ary isn't defined for that object. But that causes potential issues with objects that use #method_missing. I think #flatten should use <code>respond_to?(:to_ary)</code> to make sure an object can handle it before actually calling it.</p>
</blockquote> Ruby master - Bug #5759: flatten calls to_ary on everythinghttps://redmine.ruby-lang.org/issues/5759?journal_id=239262012-02-17T04:09:05Zddebernardy (Denis de Bernardy)ddebernardy@yahoo.com
<ul></ul><p>Possibly related:</p>
<p><a href="http://bugs.ruby-lang.org/issues/6039" class="external">http://bugs.ruby-lang.org/issues/6039</a></p> Ruby master - Bug #5759: flatten calls to_ary on everythinghttps://redmine.ruby-lang.org/issues/5759?journal_id=244692012-03-11T17:02:29Zko1 (Koichi Sasada)
<ul><li><strong>Assignee</strong> set to <i>nobu (Nobuyoshi Nakada)</i></li></ul> Ruby master - Bug #5759: flatten calls to_ary on everythinghttps://redmine.ruby-lang.org/issues/5759?journal_id=248002012-03-18T18:46:08Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li></ul> Ruby master - Bug #5759: flatten calls to_ary on everythinghttps://redmine.ruby-lang.org/issues/5759?journal_id=351542012-12-30T22:45:03Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Rejected</i></li></ul><p>When you define method_missing, you have to also define respond_to_missing? properly.</p> Ruby master - Bug #5759: flatten calls to_ary on everythinghttps://redmine.ruby-lang.org/issues/5759?journal_id=351602012-12-31T05:15:39Ztrans (Thomas Sawyer)
<ul></ul><p>=begin<br>
Isn't the the problem that it doesn't bother to check (({#respond_to?})) at all?</p>
<p>class Baz<br>
def method_missing(s)<br>
s<br>
end</p>
<pre><code>def respond_to_missing?(s, x)
return false if s == :to_ary
true
end
</code></pre>
<p>end</p>
<p>b = Baz.new<br>
b.respond_to?(:to_ary) #=> false<br>
[Baz.new].flatten<br>
=> in `flatten': can't convert Baz to Array (Baz#to_ary gives Symbol) (TypeError)</p>
<p>=end</p> Ruby master - Bug #5759: flatten calls to_ary on everythinghttps://redmine.ruby-lang.org/issues/5759?journal_id=351622012-12-31T10:46:08Zbitsweat (Jeremy Daer)jeremydaer@gmail.com
<ul></ul><blockquote>
<p>class Baz; def respond_to?(s, x) super unless s == :to_ary end end<br>
=> nil<br>
[Baz.new].flatten<br>
=> [#<a href="Baz:0x007f8d3115c7d0" class="external">Baz:0x007f8d3115c7d0</a>]</p>
</blockquote> Ruby master - Bug #5759: flatten calls to_ary on everythinghttps://redmine.ruby-lang.org/issues/5759?journal_id=351852013-01-03T12:10:19Ztrans (Thomas Sawyer)
<ul></ul><p>=begin<br>
So it does call (({#respond_to?})) after all? Yet, I thought (({#respond_to_missing?})) was invented so people would not have to override (({#respond_to?})). What's my misunderstanding? Surely we are not now expected to define both?<br>
=end</p>