DonTerry DonTerry - 5 months ago 9
Ruby Question

Trying to figure out what's wrong with this code

I can't come up with a solution.

class Person
def initialize(name)
@name = name
end

def greet(other_name)
@other_name
print "Hi #{@other_name}, my name is #{@name}"
end
end

kit = Person.new("Tom", "Jerry")
kit.greet


I would appreciate a helping hand.

Answer

You have to make a decision:

Do you want to provide both names when initializing the Person:

class Person
  def initialize(name, other)
    @name = name
    @other = other
  end

  def greet
    puts "Hi #{@other}, my name is #{@name}"
  end
end

kit = Person.new("Tom", "Jerry")
kit.greet
#=> Hi Jerry, my name is Tom

Or do you want to provide the second name when calling the greet method:

class Person
  def initialize(name)
    @name = name
  end

  def greet(other)
    puts "Hi #{other}, my name is #{@name}"
  end
end

kit = Person.new("Tom")
kit.greet("Jerry")
#=> Hi Jerry, my name is Tom
Comments