Ruby Ruby Objects and Classes Variables and Methods Attribute Writers and Accessors

MICHAEL P
MICHAEL P
5,190 Points

Error: name.rb:4:in 'initialize' : wrong number of arguments (given 1, expected 4) (ArgumentError) Please help!

Hi, even though I followed the video, and did EVERYTHING that Jason did in the video, my code does not work! I keep getting error messages.

My error code when I attempt to run name.rb :

name.rb:4:in 'initialize' : wrong number of arguments (given 1, expected 4) (ArgumentError) from name.rb:25: in 'new' from name.rb:25: in '<main>'

Here is my code. I would appreciate help!

class Name
  attr_reader :title, :first_name, :middle_name, :last_name

  def initialize(title, first_name, middle_name, last_name)
  @title = title
  @first_name = first_name
  @middle_name = middle_name
  @last_name= last_name
  end

end
  def first_name
  @first_name
  end

  def middle_name
  @middle_name
  end

  def last_name
  @last_name
end


name = Name.new("Mr.")
puts name.title + " " +
     name.first_name + " " +
     name.middle_name + " " +
     name.last_name

Moderator Edited: Added Markdown so the code is readable in the Community. Please refer the the Markdown cheatsheet when posting code.

3 Answers

Kourosh Raeen
Kourosh Raeen
23,712 Points

The initialize method expects 4 arguments but you've given it only the title. Try this:

name = Name.new("Mr.", "Jason", "", "Seifer")
MICHAEL P
MICHAEL P
5,190 Points

So, as long as I change that one line, it should be correct, right? Since "Mr." is an argument ; "Jason" is an argument ; " " is an argument ; and "Seifer" is an argument ?

Kourosh Raeen
Kourosh Raeen
23,712 Points

Sure. You can use and four strings you want.

MICHAEL P
MICHAEL P
5,190 Points

Thank you. That worked!