Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Got stuck with some Ruby code, and I'm stuck...please help?

This is actually from the Codewars website, but I have no idea what to do next:

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

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

I need to make the greet function return the expected value- but I'm stuck. Any advice?

Thanks!!

1 Answer

Maximiliane Quel
PLUS
Maximiliane Quel
Courses Plus Student 55,489 Points

I'm not entirely sure whether this is what you are asking. When you want to use the name that you initialise in the greet function you should add an @ symbol in front to indicate that it is the instance variable that you want to use.

You create a specific Person from the Person class and use the greet method on them, which will return the statement:

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

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

Max = Person.new("Max")
Max.greet("Melodie")

In this case you will get: "Hi Melodie, my name is Max".