Ruby Ruby Objects and Classes Variables and Methods Instance Variables and Local Variables

why does "name.first_and_middle_name" variable after changing it to instance variable contains only "jason"

why does "name.first_and_middle_name" variable after changing it to instance variable contains only "jason", not like what was stated inside the full_name method, where @first_and_middle_name = @first_name + " " + @middle_name + " " + @last_name , which means that name.first_and_middle_name must contains the whole name with the title "mr. jason seifer"

2 Answers

Maximiliane Quel
PRO
Maximiliane Quel
Pro Student 55,486 Points

Hi,

you are not giving me enough information to answer your question. Where do you only get jason? Why are you expecting something else? name.first_and_middle_name does not sound like a variable that should contain more than jason. If jason does not have a middle name, which it seems he doesn't if the full name is mr. jason seifer

# inside the class
@first_and_middle_name = @first_name + " " + @middle_name

#creating the object without a middle name
name = Name.new("Mr.", "Jason", "", "Seifer")
name.first_and_middle_name

# => Jason

I would only expect you to see the full name with title when you call the full name with title

name.full_name_with_title

# => Mr. Jason Seifer

I found my fault, thanks for help :)

He assigned middle_name to a empty string when initializing:

name = Name.new("Mr.", "Jason", "", "Seifer")

first_name is "Jason" middle_name is ""

definition is -> @first_and_middle_name = @first_name + " " + @middle_name -> Jason + " " + "" = Jason :)