Project

General

Profile

Bug #17424

Interactive Ruby で Object#method を再定義して任意の文字を入力するとエラーが発生する

Added by tommy.ish (智大 石井) 2 months ago. Updated 25 days ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin20]
[ruby-dev:50981]

Description

再現方法

Interactive Ruby で以下を入力した後に任意の文字を入力する。

def method
end

再現方法から得られた結果

Traceback (most recent call last):
    33: from /Users/ishii/.rbenv/versions/2.7.2/bin/irb:23:in `<main>'
    32: from /Users/ishii/.rbenv/versions/2.7.2/bin/irb:23:in `load'
    31: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/irb-1.2.6/exe/irb:11:in `<top (required)>'
    30: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb.rb:400:in `start'
    29: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb.rb:471:in `run'
    28: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb.rb:471:in `catch'
    27: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb.rb:472:in `block in run'
    26: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb.rb:537:in `eval_input'
    25: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb/ruby-lex.rb:150:in `each_top_level_statement'
    24: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb/ruby-lex.rb:150:in `catch'
    23: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb/ruby-lex.rb:151:in `block in each_top_level_statement'
    22: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb/ruby-lex.rb:151:in `loop'
    21: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb/ruby-lex.rb:154:in `block (2 levels) in each_top_level_statement'
    20: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb/ruby-lex.rb:182:in `lex'
    19: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb.rb:518:in `block in eval_input'
    18: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb.rb:704:in `signal_status'
    17: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb.rb:519:in `block (2 levels) in eval_input'
    16: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/irb/input-method.rb:294:in `gets'
    15: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/forwardable.rb:235:in `readmultiline'
    14: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/forwardable.rb:235:in `readmultiline'
    13: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/reline.rb:175:in `readmultiline'
    12: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/reline.rb:238:in `inner_readline'
    11: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/reline.rb:238:in `loop'
    10: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/reline.rb:239:in `block in inner_readline'
     9: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/reline.rb:270:in `read_io'
     8: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/reline.rb:270:in `loop'
     7: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/reline.rb:311:in `block in read_io'
     6: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/reline.rb:240:in `block (2 levels) in inner_readline'
     5: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/reline.rb:240:in `each'
     4: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/reline.rb:241:in `block (3 levels) in inner_readline'
     3: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/reline/line_editor.rb:834:in `input_key'
     2: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/reline/line_editor.rb:792:in `normal_char'
     1: from /Users/ishii/.rbenv/versions/2.7.2/lib/ruby/2.7.0/reline/line_editor.rb:718:in `process_key'
(irb):1:in `method': wrong number of arguments (given 1, expected 0) (ArgumentError)

期待する結果とその理由

この例外は起きないべきだ。何故ならソースコードに書いて実行してもエラーは出ないからだ。 irb 1.2.6 (2020-09-14) ですが irb 1.0.0 (2018-12-18) だとエラーが出ませんでした。

Updated by jeremyevans0 (Jeremy Evans) 25 days ago

  • Status changed from Open to Rejected

This isn't a bug. Reline is calling method on an object, which by default is implemented by Kernel#method. By running def method; end in the top level scope, you are defining Object#method, which takes precedence over Kernel#method. Since the method you defined does not accept arguments, while Kernel#method accepts an argument, Reline gets an ArgumentError when calling method. You can fix this issue by using a method name other than method or by defining method inside another class or module so that it does not override Kernel#method.

Also available in: Atom PDF