OO Ruby Concepts Part 3, Object Relationships

Posted by Donna Amos on April 25, 2019

alt text

Last post we gave an object its attributes so it can become its own entity. But the object we made, Garfield, can’t interact with any other objects. That’s where object relationships comes in. If objects are modeling real world entities, they need to have a method to know who they belong to. This method is called “belongs to”. Let’s start with building the “Garfield” object.


class Cat #object 
     attr_accessor :name # gives the object a name
  
   def initialize(name) # Initialize method means the name attribute is called when #new is called 
     @name = name 
   end 
end 

garfield = Cat.new("Garfield") 


Now that we have made a cat object, Garfield, we need to give him an owner. Who else will feed him lasagna?


class Owner #object 
     attr_accessor :name, :job # attributes about the object 

  def initialize(name, job) # Initialize method means the name and job attributes
    @name = name            # are called when #new is called.  
    @job = job 
  end 
end 

 jon = Owner.new("Jon", "cartoonist") # new method names the Owner, Jon, 
                                      # and gives him a job, cartoonist  

 garfield.owner = jon # Congrats! Now Garfield belongs to Jon. 

Now Garfield knows that he belongs to Jon. However, what if Jon wanted another pet? In the real world, he can own as many pets as he wants. He’s decided to get a dog. How do we give Jon another pet?

With a method is called the “has many” relationship. It allows an object to have many objects and relate to one another. In this example, it allows the Owner, Jon, to have two pets. And any more if he wants them.


Class Owner 
   attr_accessor :name, :job 

 def initialize(name, job) 
   @name = name 
   @job = job 
   @pets = [] # this empty array is where all Jon's pets will go 
 end 

 def add_pet(pet) # this method allows Jon to add as many pets as he wants
   @pets << pet # the shovels(<<) push the pet class we've created into the array
 end 

 def pets # this method simply returns the @pets array when it is called
   @pets 
 end 
end 

 jon.pets # => ["Garfield", "Odie"] this is the pets array when it is called
  

Now we need to make an Object called Pet that we can use to give Jon more pets when this method is called.


class Pet 
       attr_accessor :name, :species  

   def initialize(name, species)
    @name = name 
    @species = species 
   end 
end 

  odie = Pet.new("Odie", "dog") # "Odie" is the name, "dog" is the species
  garfield = Pet.new("Garfield", "cat") # Congrats! Jon can now adopt his two pets.

One more method to bring all this together. The self keyword refers to the owner we’re calling on.


   pet.owner = self 
 

Add this to the add_pet method so we can call the self keyword to let the pet know who his owner is.


Class Owner 
   attr_accessor :name, :job 

 def initialize(name, job) 
   @name = name 
   @job = job 
   @pets = [] 
 end 

 def add_pet(pet) 
   @pets << pet 
   pet.owner = self # self keyword 
 end 

 def pets 
   @pets 
 end 
end 


 jon.add_pet(garfield) # calling the add_pet method 
 
 garfield.owner.name # => "Jon", Garfield knows Jon is his owner 

 jon.add_pet(odie) 

 odie.owner.name # => "Jon", Odie knows Jon is his owner 

 # Now Jon has two pets and they know they belong to him!