Reflections
How do you organize all the code in a program written in Ruby? You can put your methods and lines of code into a package like a box containing similar items. You put all similar methods into a class so that they can be referenced later without typing out all the same lines of code again. In programming there is a motto "Never Repeat Yourself." You want to make the computer work for you and not the other way around. This is also why you make loops because the computer is good at repeating tasks.
Let's look at an example with ordering food.
class Dinner def initialize(appetizer, entree, dessert) @appetizer = appetizer @entree = entree @dessert = dessert end puts "Hi, welcome to The Playground" end
Here we define a class called Dinner (the name of the class must be capitalized). We create a method called method called initialize. If you have any arguments you want input into the class, this is where you label the arguments. You put the names of the arguments in parentheses after "initialize" with a comma after each argument if there is more than one. Here I have "appetizer", "entree", and "dessert". In order to use these variables across all methods anywhere in this Dinner class, I have to use an instance variable. This is denoted by an "@" before a variable name. So I created 3 instance variables equal to the arguments the user inputs when creating the class. When I run the program nothing happens. Nothing happens because I haven't called the class.
We can do that by adding this line outside the class and running it:
the_playground = Dinner.new("Calamari", "Maple Glazed Porkchop", "Black Mission Fig Sticky Toffee Pudding") #=> Hi, welcome to The Playground
Here, we created a new object with the class "Dinner" and put 3 arguments. We saved this to the variable "the_playground". This is actually the name of one my favorite restaurants in Southern California. It displays the welcome message when we ran it.
Let's make it print out the items that we ordered.
class Dinner def initialize(appetizer, entree, dessert) @appetizer = appetizer @entree = entree @dessert = dessert end puts "Hi, welcome to The Playground" def server puts "What would you like to have this evening?" end def order puts "I would like to have the #{@appetizer} for an appetizer." puts "I would like to have the #{@entree} for my main course." puts "I would like to have the #{@dessert} for dessert." end end the_playground = Dinner.new("Calamari", "Maple Glazed Porkchop", "Black Mission Fig Sticky Toffee Pudding") the_playground.server the_playground.order #=> Hi, welcome to The Playground What would you like to have this evening? I would like to have the Calamari for an appetizer. I would like to have the Maple Glazed Porkchop for my main course. I would like to have the Black Mission Fig Sticky Toffee Pudding for dessert.
We added a new method called "order" that puts the dish you want to order. In each sentence we reference the instance variable, which can be use used across methods in the same class.
We also added two more lines at the bottom that called the method "server" and "order" on "the_playground" variable. These methods that we defined within the class are called instance methods. These methods are only accessible to an object that is "Dinner" class. They can't be used outside the class.
Classes keep your code organized. You can think each class as mini specialized robots. Each class has a set of instructions like a robot. Each robot is specialized to do its own set of tasks. You can call on one robot or a couple robots to do certain tasks. This is much more efficient then making one big robot to do everything. In the programming world, it's much easier to change or fix an error in a specific class than in a huge program.
By the way, I have no idea how I went from food to robots. Maybe in the future, robots will be serving you food.