Issue #5759 has been updated by trans (Thomas Sawyer).


=begin
Isn't the the problem that it doesn't bother to check (({#respond_to?})) at all?

  class Baz
    def method_missing(s)
      s
    end

    def respond_to_missing?(s, x)
      return false if s == :to_ary
      true
    end
  end

  b = Baz.new
  b.respond_to?(:to_ary)  #=> false
  [Baz.new].flatten
  => in `flatten': can't convert Baz to Array (Baz#to_ary gives Symbol) (TypeError)

=end

----------------------------------------
Bug #5759: flatten calls to_ary on everything
https://bugs.ruby-lang.org/issues/5759#change-35160

Author: trans (Thomas Sawyer)
Status: Rejected
Priority: Normal
Assignee: nobu (Nobuyoshi Nakada)
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.



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