ncaskey04 / family_tree

a ruby inheritance lab

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reading

As an added resource feel free to read the following: ruby monk ascent inheritance and more classes

Family Tree Lab

For this lab you are going to use your knowledge of Classes, inheritance and rspec to build a small family tree app and pass a bunch of tests.

Getting started

  1. Start working on the Grandparent class tests and try to get them to pass one by one. To do this you will need to research how to test if something is an instance of a class.
  2. The Grandparent should have a class variable called @@grandchildren, that will store their grandchildren count, which should start at 0 as well as a class variable called @@family which should be an empty array, that will later store their family members.
  3. The Grandparent class should have class methods to return the number of grandchildren and the family array.
  4. On the initialize method, add the initialized instance to the family array
  5. Finally, the Grandparent class should have a class method called showFamily which loops over the @@family array and displays each family member as a hash with the following keys (relation, name, age). The relation will be whatever Class you have added (either Grandparent, Parent or Child). To do this, you will have to look up how to see what Class something is.

Next steps

Once you have gotten these tests to pass, move onto the parent class:

  1. Look at your test files in the spec folder and start writing tests that fail.
  2. Have a Parent inherit from the Grandparent class. Tip: you can use super to easily initialize instances of the Parent class.
  3. Similarly, make sure the parent Class has a class variable called @@children which will start at 0.
  4. Create a getter method that returns the @@children
  5. Write the necessary code to make your tests pass

Finishing up

Once you've completed the Parent class, finish with the Child class which should inherit from Parent.

  1. First take a look at the tests and write failing tests.
  2. Once again, make sure the Child inherits from Parent, and use super to DRY up your initialize method. Assign the Child class a class variable called @@favColors which should be an array of colors.
  3. When the Child is created, randomly pick a color from the array and assign it to the instance variable @favColor. Tip: ruby has a method which randomly samples a value from an array.
  4. Write a getter method that returns the array of colors.
  5. Write a method called growUp which when called increments the instance's variable @age by one.
  6. Finally write a method 'talk' which will take one parameter called text and return a message depending on the child's age
    • if the child is older than 2, have your method display the value of the text parameter and the child's favorite color
    • if the child is under 2, have your method return "Wahhh!"
    • give your text parameter a default value of nil so that you can call 'talk' without an error. This will require you to look up how to assign default values for a parameter.

About

a ruby inheritance lab


Languages

Language:Ruby 100.0%