Bird
0
0

How would you add an instance method full_name to this Ruby class that returns the concatenation of @first_name and @last_name separated by a space?

hard📝 Application Q8 of 15
Ruby - Classes and Objects

How would you add an instance method full_name to this Ruby class that returns the concatenation of @first_name and @last_name separated by a space?

class Member
  def initialize(first, last)
    @first_name = first
    @last_name = last
  end
end
Adef full_name first_name + last_name end
Bdef full_name @first_name + @last_name end
Cdef full_name "#{@first_name} #{@last_name}" end
Ddef full_name "#{first_name} #{last_name}" end
Step-by-Step Solution
Solution:
  1. Step 1: Access instance variables

    Instance variables @first_name and @last_name hold the names.
  2. Step 2: Concatenate with space

    Use string interpolation to combine them with a space: "\#{@first_name} \#{@last_name}".
  3. Step 3: Check options

    def full_name "#{@first_name} #{@last_name}" end correctly returns the full name with space. def full_name @first_name + @last_name end misses space. Options C and D use undefined local variables.
  4. Final Answer:

    def full_name "#{@first_name} #{@last_name}" end -> Option C
  5. Quick Check:

    Use instance variables with interpolation for full name. [OK]
Quick Trick: Use '@' variables and interpolation to combine names. [OK]
Common Mistakes:
  • Omitting space between names
  • Using variables without '@' prefix
  • Concatenating without space

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes