ga-wdi-boston / ruby-object-class-methods

Other
0 stars 130 forks source link

General Assembly Logo

Ruby Class Methods

Prerequisites

Objectives

By the end of this, developers should be able to:

Preparation

  1. Fork and clone this repository. FAQ
  2. Create a new branch, training, for your work.
  3. Checkout to the training branch.
  4. Install dependencies with bundle install.

Demo: Class Methods

Sometimes a method is associated with a class, not an object.

Observe how a Method on a Class differs from a method on an Instance that Class.

Let's discuss the code below in lib/pet.rb

In the Animal class, and the ::new method is recieved by the Animal class, with 'Zeno' and 'cat' as an arguments. It then calls the instance method #initialize with 'Zeno' and 'cat' as an arguments, which assigns 'Zeno' to an instance variable, @name and 'cat' to the instance variable @species.

The Pet class is different. The Pet class is used here to give us meta-data about a list of Pets. When we get to Rails, this data will be information we get back from the database.

Where will the method dog_count be recieved? What about all?

Lab: Make a Class Method

Bonus:

License

  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.