gpt4 book ai didi

ruby - ArgumentError : wrong number of arguments (given 0, 预期 1) Ruby

转载 作者:行者123 更新时间:2023-12-03 23:12:50 24 4
gpt4 key购买 nike

ArgumentError: wrong number of arguments (given 0, expected 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 args 传递:

send(search_method, arg1, arg2)

你的情况

send(search_method, paragraph)

Docs

关于ruby - ArgumentError : wrong number of arguments (given 0, 预期 1) Ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57717165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com