Created
February 6, 2015 19:39
-
-
Save joey-g/fc4b4f69b73c79c3804f to your computer and use it in GitHub Desktop.
Automation Training - Cat Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The class 'Cat' defines the blueprint of what an instantiated Cat | |
# object will eventually look like. In this case, a Cat object would | |
# have two attributes (name and color), and one function (or verb) that | |
# can be called on that object (eat_food). | |
class Cat | |
# Attributes that describe the Cat class. | |
attr_reader :name | |
attr_reader :color | |
# Constructor that is responsible for 'building' an instance of the | |
# Cat class. Here, the constructor was passed two variables, name and color | |
# to be set as attributes on an instance of the Cat class. | |
def initialize(name, color) | |
@name = name | |
@color = color | |
end | |
# Function that describes some sort of action that a Cat object will take. | |
# In this example, eat_food will call the 'puts' function, passing in a String | |
# parameter. The puts function is a built-in Ruby routine that puts a String | |
# to the console. | |
def eat_food | |
puts('Cat object is eating.') | |
end | |
end | |
cat1 = Cat.new('Bruce', 'Grey') # <--- Creates a new instance of the Cat object | |
# Passes in String parameters 'Bruce' and 'Grey' | |
# to be stored as attributes on the newly | |
# instantiated object. | |
# cat1 is a variable (or bucket) for storing values. | |
# In this case, cat1 will hold or store a newly created | |
# instance of the Cat class. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment