在 irb 中定义一个名为 'method' 的方法时,出现了错误的参数个数

In irb while defining a method named 'method', wrong number of argument apperaed

after define a method named 'method' in irb/gry

def method
    puts 1
end
 Traceback (most recent call last):
        33: from F:/Ruby27-x64/bin/irb.cmd:31:in `<main>'
        32: from F:/Ruby27-x64/bin/irb.cmd:31:in `load'
        31: from F:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/irb-1.2.3/exe/irb:11:in `<top (required)>'
        30: from F:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:399:in `start'
        29: from F:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:470:in `run'
        28: from F:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:470:in `catch'
        27: from F:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:471:in `block in run'
        26: from F:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:536:in `eval_input'
        25: from F:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:134:in `each_top_level_statement'
        24: from F:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:134:in `catch'
        23: from F:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:135:in `block in each_top_level_statement'
        22: from F:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:135:in `loop'
        21: from F:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:138:in `block (2 levels) in each_top_level_statement'
        20: from F:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:166:in `lex'
        19: from F:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:517:in `block in eval_input'
        18: from F:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:695:in `signal_status'
        17: from F:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:518:in `block (2 levels) in eval_input'
        16: from F:/Ruby27-x64/lib/ruby/2.7.0/irb/input-method.rb:262:in `gets'
        15: from F:/Ruby27-x64/lib/ruby/2.7.0/forwardable.rb:235:in `readmultiline'
        14: from F:/Ruby27-x64/lib/ruby/2.7.0/forwardable.rb:235:in `readmultiline'
        13: from F:/Ruby27-x64/lib/ruby/2.7.0/reline.rb:174:in `readmultiline'
        12: from F:/Ruby27-x64/lib/ruby/2.7.0/reline.rb:236:in `inner_readline'
        11: from F:/Ruby27-x64/lib/ruby/2.7.0/reline.rb:236:in `loop'
        10: from F:/Ruby27-x64/lib/ruby/2.7.0/reline.rb:237:in `block in inner_readline'
         9: from F:/Ruby27-x64/lib/ruby/2.7.0/reline.rb:266:in `read_io'
         8: from F:/Ruby27-x64/lib/ruby/2.7.0/reline.rb:266:in `loop'
         7: from F:/Ruby27-x64/lib/ruby/2.7.0/reline.rb:307:in `block in read_io'
         6: from F:/Ruby27-x64/lib/ruby/2.7.0/reline.rb:238:in `block (2 levels) in inner_readline'
         5: from F:/Ruby27-x64/lib/ruby/2.7.0/reline.rb:238:in `each'
         4: from F:/Ruby27-x64/lib/ruby/2.7.0/reline.rb:239:in `block (3 levels) in inner_readline'
         3: from F:/Ruby27-x64/lib/ruby/2.7.0/reline/line_editor.rb:837:in `input_key'
         2: from F:/Ruby27-x64/lib/ruby/2.7.0/reline/line_editor.rb:795:in `normal_char'
         1: from F:/Ruby27-x64/lib/ruby/2.7.0/reline/line_editor.rb:721:in `process_key'
(irb):1:in `method': wrong number of arguments (given 1, expected 0) (ArgumentError)

but if I change its name, the bug fixed

改名后,运行正确

我认为这个错误的原因是 ruby 中的方法名称不能 'method' 但在 RubyMine 2020.2 中尝试后,它运行正确 我很困惑。

在 RubyMine 中

您正在覆盖 Object#method 方法。 从不 覆盖核心 Ruby 方法是个好主意。

特别是,IRb 和 Pry 都支持 auto-completion 在您键入时,它们支持文档和许多其他内容。猜猜他们使用什么方法来获得可以使用的 Method 对象?

所以,发生的事情是 IRb 正在调用 method(some_method_name) 来访问某些方法,但是您已经用不带参数的方法覆盖了 Object#method 方法。

不要乱用 Ruby 的核心方法。如果您正在使用 IRb,尤其是不要混淆 IRb 使用的核心方法。