Bug #8870

Method-Missing breaking irb

Added by Ayodele Abejide 7 months ago. Updated 7 months ago.

[ruby-core:57047]
Status:Open
Priority:Normal
Assignee:-
Category:-
Target version:-
ruby -v:ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0] Backport:1.9.3: UNKNOWN, 2.0.0: UNKNOWN

Description

2.0.0p247 :001 > def methodmissing m,*args; [m.tos,args].flatten.join " "; end
=> nil
2.0.0p247 :002 > "j".happy "hh"

2.0.0p247 :001 > def method_missing *args; args.join " "; end
=> nil
2.0.0p247 :002 > "j".happy
SystemStackError: stack level too deep
from /Users/bjhaid/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/workspace.rb:86
Maybe IRB bug!

2.0.0p247 :003 > def method_missing m,*args; args.join " "; end
Segmentation fault: 11

2.0.0p247 :001 > def method_missing m,*args; [m,args].flatten " "; end
=> nil
2.0.0p247 :002 > "j".happy hah
Segmentation fault: 11

History

#1 Updated by Nobuyoshi Nakada 7 months ago

Array#flatten tries to call #toary method on non-Array arguments, but the result of `m.tos' doesn't have it, so the method_missing is called, and continues infinitely.

In short, do not redefine global method_missing method.

#2 Updated by Ayodele Abejide 7 months ago

I do understand that its bad to use method_missing in the global space, but the behavior should be same irrespective of scope

This works:
2.0.0-p247 :002 > m "2","a","b"
=> ["2", "a", "b"]
2.0.0-p247 :003 > (m "2","a","b").class
=> Array

This breaks:
2.0.0-p247 :004 > def methodmissing m,*args; args.class; end
=> nil
2.0.0-p247 :005 > m "2","a","b"
/Users/bjhaid/.rvm/scripts/irbrc.rb:32:in `initialize': can't convert String to Fixnum (String#to
int gives Class) (TypeError)

#3 Updated by Nobuyoshi Nakada 7 months ago

I know nothing about what your script does.

Also available in: Atom PDF