Interacting Objects
Learn how objects can interact with each other by defining appropriate class methods .
Objects that interact
We’re now able to create our own objects. However, they don’t do a whole lot yet. Why not create two people, and let them greet each other?
Here’s what we’d like to achieve:
Press + to interact
Ruby
class Persondef initialize(name)@name = nameenddef name@nameendendperson = Person.new("Anja")friend = Person.new("Carla")person.greet(friend)
If we run the code above, we get an error message that tells us that the greet
method needs to be defined.
To begin, we’d like this method to print out the following:
Hi Carla!
If we run the code above, we’ll get an error message that tells us what to do next:
NoMethodError: undefined method `greet' for #<Person:0x007fbb5e9c88c8 @name="Anja">
We need to define a greet
...