簡體   English   中英

ArgumentError:參數數量錯誤(給定 0,預期為 1)Ruby

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

ArgumentError:參數數量錯誤(給定 0,預期為 1)。

代碼打開文件,查看段落並計數,錯誤在代碼的中心。 調用方法時發生錯誤 (1)。 我無法理解如何傳遞參數方法。

@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))

我無法理解如何將參數傳遞給這個地方。

                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

如果你調用send(search_method)你調用一個沒有參數的方法。 要將參數傳遞給被調用的方法,您需要將它們作為下一個send參數傳遞:

send(search_method, arg1, arg2)

在你的情況下

send(search_method, paragraph)

文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM