Giter Site home page Giter Site logo

ruby-inheritance-lab-onl01-seng-pt-012320's Introduction

Ruby Inheritance Lab

Objectives

  1. Define classes that inherit from a shared parent, or super, class.
  2. Define methods unique to those classes.

Overview

In this lab, we'll be working with a school domain model. Our application has users that are either teachers or students. Teachers and students will share certain attributes and have certain behaviors that are unique to them. You'll be defining a User class that both students and teachers inherit from and you'll be writing methods within both the Teacher and Student class that are unique to that class.

Instructions

Run the test suite to get started. This is a test-driven lab.

  1. Define the User class such that a user can have a first and last name. You'll need both a setter and a getter for first and last name.
  2. We've given you a barebones Teacher class in lib/teacher.rb. Change the class definition so that the Teacher class inherits from the User class. Run the test suite and notice that you are passing some tests for the Teacher class, even without writing any code inside that class. That is because it will inherit the #first_name and #last_name methods from the User class you told it to inherit from.
  3. We've given you a class constant KNOWLEDGE, that points to an array of knowledge strings. Write a method, #teach that returns a random element from that array. You might want to look up the Array#sample method.
  4. We've given you a barebones Student class. Change the class definition so that it inherits from the User class. Run the test suite and notice that you are passing some tests for the Student class, even without writing any code inside that class. That is because it will inherit the #first_name and #last_name methods from the User class you told it to inherit from.
  5. Individual students should initialize with an instance variable, @knowledge, that points to an empty array.
  6. Define a method, #learn, that takes in a string and adds that string to the student's @knowledge array.
  7. Define a method, #knowledge, that returns that student's knowledge array.

Bonus

We've set up an executable file for you in bin/time_for_school. Check out the code there and run the file with ruby bin/time_for_school in your terminal. Get a feel for how the code we wrote works and what methods are available to our student and teacher instances thanks to inheritance.

Play around with it a bit more by trying out the following in this file:

  • We have a new user, Jim, who is just here to audit a few classes. He is neither a teacher nor a student. Create a local variable, jim, and point it equal to an instance of the User class. Then, give jim a first and last name. What happens when you try to call #learn on jim?
  • Continue to operate on our student, steve. Have avi teach him some more important knowledge.
  • Create a new student and have that student learn a few things from avi as well.
  • Then, puts out the existing knowledge of each student.

View Ruby Inheritance Lab on Learn.co and start learning to code for free.

View Intro to Inheritance Lab on Learn.co and start learning to code for free.

ruby-inheritance-lab-onl01-seng-pt-012320's People

Contributors

annjohn avatar ga-be avatar gj avatar jmburges avatar jonbf avatar maxwellbenton avatar sophiedebenedetto avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.