Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Objects and Classes Variables and Methods Variables

MICHAEL P
MICHAEL P
5,191 Points

My code is not passing, when I follow the video. Please help.

Hi, My code is not passing, when I follow the video. Please help.

class 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

def title @title end

def first_name @first_name end

def middle_name @middle_name end

def last_name @last_name end end

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

I get the following error message, and I do not understand why.

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

2 Answers

John Coolidge
John Coolidge
12,614 Points

Near the bottom of your code when you create the following:

name = Name.new("Mr.")

The error says you are only supplying one argument and it's expecting four. The reason it's expecting four is because of this code snippet near the top:

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

The initialize method has four arguments, and so when you create an instantiation of the class Name (as shown above) you need to add the necessary arguments:

name = Name.new("Mr.", "Joe", "William", "Simpson")

I hope that helps you understand the error and how to fix it in the future.

MICHAEL P
MICHAEL P
5,191 Points

Thank you John! That really helped! Your explanation made much better sense than the video!