Class Syntax
We’ve talked at a high level about classes, instances and attributes, and you may be starting to wonder what this will look like in code!
Explore Class Syntax
While this may feel uncomfortable at first, research shows this is one of the most effective ways to learn. Instead of your instructor typing code and explaining every character, you are going to read some Ruby code that's already been written. You will likely have some questions about what X or Y is doing, and you'll also likely be able to make some deductions about what other pieces are doing. We will talk through it all after you've had a chance to push your brain and make some connections on your own.
Look at the code in this repl.it and think through the guiding questions:
- What is the name of the class?
- How many instances are being made?
- What is the attribute?
- Can you make another instance?
Create a Candy Bowl
Now that you've seen an example of a class, let's work on making two classes - Candy and CandyBowl. We'll use the structure of Candy that we discussed earlier (candy should have a size and type) and our CandyBowl should be able to hold and dispense candy.
Some things to think about:
- What attributes (state) should our classes have?
- What behaviors (methods) should our classes have?
Connection to Apps we Use
Now that you’ve seen how this “factory” concept can be used in code, you may be wondering about how this code concept is used within apps we use every day.
We won’t get all the way there in illustrating it today, but, we can look at Instagram for a moment to talk through where we see some use of classes and instances.