Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

ArgumentError: wrong number of arguments (given 0, expected 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). 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
like image 222
Denis L Avatar asked Aug 11 '26 23:08

Denis L


1 Answers

If you call send(search_method) you call a method without arguments. To pass arguments to the method being called, you need to pass them as next send args:

send(search_method, arg1, arg2)

in your case

send(search_method, paragraph)

Docs

like image 92
mrzasa Avatar answered Aug 14 '26 08:08

mrzasa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!