这是我的第一个ruby应用程序。我是一个堆积如山的处女…当我运行以下程序时:

class NameApp

def intialize(name)
    @names = []
end

def name_question
    print "What is your name? "
    answer = gets.chomp
    @names += answer.to_s
    puts "The number of characters in your name is " + names.length
end


def name_length
    if @names.length > 25 then
        print "Your name is longer than 25 characters."
    else
        print "Your name is too short."
    end
end

end

name_app = NameApp.new("Test1")
name_app.class # => NameApp

name_app.name_question
name_app.name_length

我得到这个简单的错误消息结果:
name.rb:26:in `initialize': wrong number of arguments(1 for 0) (ArgumentError)
from nameapp.rb:26:in `new'
from nameapp.rb:26:in `<main>'

你能帮我排除故障吗?

最佳答案

由于您尚未为initialize定义方法NameApp,因此默认情况下,它接受零个参数,但您通过构造函数"Test1"传递了一个参数new

关于ruby - 获取`initialize':错误的参数数量(1为0)(ArgumentError)对于简单的ruby应用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18679769/

10-13 04:46