Forum: Ruby-core [ruby-trunk - Bug #5759][Open] flatten calls to_ary on everything

Posted by Thomas Sawyer (7rans)
on 2011-12-14 00:56
(Received via mailing list)
Issue #5759 has been reported by Thomas Sawyer.

----------------------------------------
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.
Posted by Eric Hodel (Guest)
on 2011-12-14 02:06
(Received via mailing list)
Issue #5759 has been updated by Eric Hodel.


Use Kernel#Array:

  $ ruby -e 'p Array("a\nb"), Array(["a\nb"])'
  ["a\nb"]
  ["a\nb"]

----------------------------------------
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.
Posted by Alex Young (regularfry)
on 2011-12-14 11:15
(Received via mailing list)
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
Posted by Denis de Bernardy (Guest)
on 2012-02-16 20:09
(Received via mailing list)
Issue #5759 has been updated by Denis de Bernardy.


Possibly related:

http://bugs.ruby-lang.org/issues/6039
----------------------------------------
Bug #5759: flatten calls to_ary on everything
https://bugs.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.
Posted by Nobuyoshi Nakada (nobu)
on 2012-12-30 14:45
(Received via mailing list)
Issue #5759 has been updated by nobu (Nobuyoshi Nakada).

Status changed from Assigned to Rejected

When you define method_missing, you have to also define 
respond_to_missing? properly.
----------------------------------------
Bug #5759: flatten calls to_ary on everything
https://bugs.ruby-lang.org/issues/5759#change-35154

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.
Posted by Thomas Sawyer (7rans)
on 2012-12-30 21:15
(Received via mailing list)
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.
Posted by bitsweat (Jeremy Kemper) (Guest)
on 2012-12-31 02:46
(Received via mailing list)
Issue #5759 has been updated by bitsweat (Jeremy Kemper).


> class Baz; def respond_to?(s, x) super unless s == :to_ary end end
=> nil
> [Baz.new].flatten
=> [#<Baz:0x007f8d3115c7d0>]
----------------------------------------
Bug #5759: flatten calls to_ary on everything
https://bugs.ruby-lang.org/issues/5759#change-35162

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.
Posted by Thomas Sawyer (7rans)
on 2013-01-03 04:10
(Received via mailing list)
Issue #5759 has been updated by trans (Thomas Sawyer).


=begin
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?
=end

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

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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.