Project

General

Profile

Bug #5158

to_ary not respecting respond_to?

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

Status:
Closed
Priority:
Normal
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/

Associated revisions

Revision 55148a25
Added by nobu (Nobuyoshi Nakada) over 8 years ago

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

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32855 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 32855
Added by nobu (Nobuyoshi Nakada) over 8 years ago

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

Revision 32855
Added by nobu (Nobuyoshi Nakada) over 8 years ago

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

Revision 32855
Added by nobu (Nobuyoshi Nakada) over 8 years ago

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

Revision 32855
Added by nobu (Nobuyoshi Nakada) over 8 years ago

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

Revision 32855
Added by nobu (Nobuyoshi Nakada) over 8 years ago

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

Revision 32855
Added by nobu (Nobuyoshi Nakada) over 8 years ago

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

Revision afcd5562
Added by nobu (Nobuyoshi Nakada) almost 8 years ago

merge revision(s) r32855,r32857,r33493,r34554:

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

    * test/ruby/test_object.rb: tests that respond_to? returns false.

    * vm_eval.c (check_funcall): set array elements one-by-one to fix
      compile error with Fujitsu C Compiler 5.6 on Solaris 10 on Sparc.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@34615 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

History

Updated by naruse (Yui NARUSE) over 8 years ago

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

Updated by nobu (Nobuyoshi Nakada) over 8 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 8 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 8 years ago

On Thu, Aug 4, 2011 at 11:37 PM, Ryan Davis ryand-ruby@zenspider.comwrote:

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 8 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
adgar@carboni.ca
http://carboni.ca/

Also available in: Atom PDF