Skip to Content
Learn
Object-Oriented Programming II
Quick Review: Building a Class

Let’s build a class!

class ClassName def method_name(parameter) @class_variable = parameter end end
  1. First we tell Ruby that we want to construct our class ClassName. Don’t forget the end to end your class!
  2. Inside our ClassName is where we define our methods. Here we have method_name followed by (parameter), When we use this method it will look like this method_name("hello!")
  3. If our parameter above were “Hello!” it would be assigned to our variable @class_variable on the following line.
  4. Again, don’t forget to end your method_name!

Instructions

1.

First, create a class named Dog

Create a method initialize within your class Dog.

Allow your initialize method to accept two parameters, name and breed.

Inside your initialize method assign those two parameters to the variables @name and @breed respectively

Folder Icon

Take this course for free

Already have an account?