Project

General

Profile

Actions

Bug #5158

closed

to_ary not respecting respond_to?

Added by zenspider (Ryan Davis) over 12 years ago. Updated over 12 years ago.

Status:
Closed
Target version:
-
ruby -v:
-
Backport:
[ruby-core:38799]

Description

class BadProxyObject
def initialize
@p = Object.new
def @p.to_ary
raise "I should never be called"
end
end

def respond_to?(*a)
p :bad_respond_to? => a
false
end

def method_missing(msg, *)
p :bad_method_missing => msg
@p.send msg
end
end

[[BadProxyObject.new]].flatten

{:bad_method_missing=>:to_ary}

Exception `RuntimeError' at wtf.rb:6 - I should never be called

wtf.rb:6:in `to_ary': I should never be called (RuntimeError)

from wtf.rb:17:in `method_missing'

from wtf.rb:21:in `flatten'

from wtf.rb:21:in `'

Extra Info: http://tenderlovemaking.com/2011/06/28/til-its-ok-to-return-nil-from-to_ary/

Updated by naruse (Yui NARUSE) over 12 years ago

  • Status changed from Open to Assigned
  • Assignee set to nobu (Nobuyoshi Nakada)
Actions #2

Updated by nobu (Nobuyoshi Nakada) over 12 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r32855.
Ryan, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • vm_eval.c (check_funcall): try respond_to? first if redefined.
    [Bug #5158]

Updated by aprescott (Adam Prescott) over 12 years ago

And I've just seen that http://redmine.ruby-lang.org/issues/5158 has been
opened and closed. Too slow, I guess.

Updated by aprescott (Adam Prescott) over 12 years ago

On Thu, Aug 4, 2011 at 11:37 PM, Ryan Davis wrote:

Extra Info:
http://tenderlovemaking.com/2011/06/28/til-its-ok-to-return-nil-from-to_ary/

Someone made a comment on that post linking to
http://yehudakatz.com/2010/01/02/the-craziest-fing-bug-ive-ever-seen/ which
explains, specifically for Array#flatten:


In Ruby 1.8, the process is essentially the following:

if obj.respond_to?(:to_ary)
obj.send(:to_ary)
else
obj
end

In Ruby 1.9, it was changed to:

begin
obj.send(:to_ary)
rescue NoMethodError
obj
end


I'm not sure why that changed happened, but at least that seems to be the
source of the behaviour.

Updated by Anonymous over 12 years ago

On Aug 5, 2011, at 6:06 AM, Adam Prescott wrote:

I'm not sure why that changed happened, but at least that seems to be the source of the behaviour.

I believe it happened because respond_to? is unreliable in the presence of
method_missing unless a lot of care is taken, and that care is rarely taken.

Michael Edgar

http://carboni.ca/

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0