Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

MICHAEL P
MICHAEL P
5,191 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,733 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,191 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,733 Points

Sure. You can use and four strings you want.

MICHAEL P
MICHAEL P
5,191 Points

Thank you. That worked!