From: Alex Young Date: 2011-12-14T19:14:57+09:00 Subject: [ruby-core:41648] Re: [ruby-trunk - Bug #5759] flatten calls to_ary on everything On 14/12/11 01:05, Eric Hodel wrote: > > Issue #5759 has been updated by Eric Hodel. > > > Use Kernel#Array: > > $ ruby -e 'p Array("a\nb"), Array(["a\nb"])' > ["a\nb"] > ["a\nb"] Or a splat: ruby-1.9.3-p0 :001 > a="a\nb"; [*a] => ["a\nb"] ruby-1.9.3-p0 :002 > a=["a\nb"]; [*a] => ["a\nb"] Not sure I disagree that #flatten should check first (or just leave the exception uncaught) though. -- Alex > > ---------------------------------------- > Bug #5759: flatten calls to_ary on everything > http://redmine.ruby-lang.org/issues/5759 > > Author: Thomas Sawyer > Status: Open > Priority: Normal > Assignee: > Category: > Target version: 1.9.3 > ruby -v: ruby 1.9.3dev (2011-09-23 revision 33323) [x86_64-linux] > > > I often ensure that I have an array by doing: > > def foo=(x) > @foo = [x].flatten > end > > 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 `respond_to?(:to_ary)` to make sure an object can handle it before actually calling it. > > >