Ruby Ruby Objects and Classes Variables and Methods Variables

MICHAEL P
MICHAEL P
5,190 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,613 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,190 Points

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