简体   繁体   English

ArgumentError:参数数量错误(给定 0,预期为 1)Ruby

[英]ArgumentError: wrong number of arguments (given 0, expected 1) Ruby

ArgumentError: wrong number of arguments (given 0, expected 1). ArgumentError:参数数量错误(给定 0,预期为 1)。

The code opens the file and looks at the paragraph and counts, the error is in the center of the code.代码打开文件,查看段落并计数,错误在代码的中心。 An error occurs when a method is called(1).调用方法时发生错误 (1)。 I can't understand how to pass the argument methods.我无法理解如何传递参数方法。

@books = "You can use this knowledge to create small tools that might help."

require "colorize"

class Filecalculation

    def select
        loop do
            puts "# Will we search : calculation_lines paragraph(1)".cyan
            print "\n>>>>>> ".yellow

            input = gets.chomp
            search_method = "calc_#{input}"
            if (respond_to?(search_method))

I can't understand how to pass the argument to this place.我无法理解如何将参数传递给这个地方。

                contents = send(search_method, @books)
            else
                puts "Unknown input: #{input.inspect}, method #{search_method} not defined."
            end 
       end 
    end  

    # =================== calc_1 сounting words in Text File 
    def calc_1 paragraph            
        word_count = paragraph.split.length 
        puts "#{word_count} words"   
    end
end

Filecalculation.new.select

If you call send(search_method) you call a method without arguments.如果你调用send(search_method)你调用一个没有参数的方法。 To pass arguments to the method being called, you need to pass them as next send args:要将参数传递给被调用的方法,您需要将它们作为下一个send参数传递:

send(search_method, arg1, arg2)

in your case在你的情况下

send(search_method, paragraph)

Docs文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby - ArgumentError:参数数量错误(给定 3,预期为 2) - Ruby - ArgumentError: wrong number of arguments (given 3, expected 2) ArgumentError:参数数量错误(给定 0,预期为 1) - Ruby - ArgumentError: wrong number of arguments (given 0, expected 1) - Ruby 在 Ruby 中使用 Expect 的参数数量错误(给定 0,预期为 1)(ArgumentError) - wrong number of arguments (given 0, expected 1) (ArgumentError) with Expect in Ruby RSpec - 如何修复 - ArgumentError:参数数量错误(给定 0,预期为 1) - Ruby - RSpec - how to fix - ArgumentError: wrong number of arguments (given 0, expected 1) - Ruby Ruby错误:参数数量错误(给定4个,预期为0)(ArgumentError) - Ruby Error: wrong number of arguments (given 4, expected 0) (ArgumentError) Ruby:枚举的最大值抛出`ArgumentError:参数数量错误(给定1,预期为0)` - Ruby: max of enumerize throw `ArgumentError: wrong number of arguments (given 1, expected 0)` 一个简单的 Ruby 函数的“ArgumentError(参数数量错误(给定 2,预期为 1))” - "ArgumentError (wrong number of arguments (given 2, expected 1))" for a simple Ruby function Ruby ArgumentError:arguments 的错误编号(给定 2,预期 0..1) - Ruby ArgumentError: wrong number of arguments (given 2, expected 0..1) 参数数量错误(给定1,预期为2)(ArgumentError) - wrong number of arguments (given 1, expected 2) (ArgumentError) Jekyll:参数数量错误(给定 2,预期为 1)(ArgumentError) - Jekyll: wrong number of arguments (given 2, expected 1) (ArgumentError)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM