Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting `initialize': wrong number of arguments(1 for 0) (ArgumentError) for simple ruby app

Tags:

arguments

ruby

This is my first ruby app. And I am a stack overflow virgin... When I run the following program:

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

I get this simple error message result:

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>'

Can you help me trouble shoot?

like image 455
nilesvm Avatar asked Sep 08 '13 02:09

nilesvm


2 Answers

You spelled "initialize" wrong. I did that a few times too when I was starting out, and that was hard to debug. Why ruby didn't name it "init", I'll never know.

like image 136
7stud Avatar answered Oct 05 '22 12:10

7stud


Since you have not defined the method initialize for NameApp, by default, it takes zero arguments, but you passed one argument "Test1" via the constructor new.

like image 25
sawa Avatar answered Oct 05 '22 13:10

sawa