nilesvm
nilesvm

Reputation: 339

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

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?

Upvotes: 14

Views: 23426

Answers (4)

Peter Brendel
Peter Brendel

Reputation: 51

If you're reading this thread and you spelled initialize correctly the issue might be related to your super call.

This fails:

def initialize(bar:)
  super
  @bar = bar
end

And this works:

def initialize(bar:)
  super()
  @bar = bar
end

Upvotes: 1

samanthi22
samanthi22

Reputation: 11

For require_relative 'user' move old 'user.rb' up one level rename 'user2.rb' to 'user.rb'. Also, there is a typo.

Upvotes: 0

7stud
7stud

Reputation: 48599

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.

Upvotes: 71

sawa
sawa

Reputation: 168121

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.

Upvotes: 9

Related Questions