Today's Code Another version of Today's Code
The goal of this breakout is to introduce OOP (Object Oriented Programming) in Ruby.
We will attempt to model a real life pokemon battle which discusses the following concepts:
What's a class? When do we use classes?
- A blueprint for a KIND of object. When we want to describe what an object HAS, and what it can DO.
What's the difference between a class and an object?
- An object is an INSTANCE of a class. NOTE: Ruby object !== JS object. Ruby objects are more strict.
What does the initialize method do?
- constructor
What are Instance variables and how are they used?
- Ruby's version of
this.<THING>
, making it so that a variable (property) can be accessed/manipulated with different methods in a class
What are attribute accessors and readers?
- Allow the instance to EXPOSE variables (either to read or manipulate)
What are Instance methods and how are they used?
- things that the instance can DO! (e.g. attack)
- Methods that are exposed
What are class methods & variables? What's the difference between them and instance methods & variables?
- Look at variables named with @@ online
- Look at self.METHODNAME
Inheritance in Ruby classes
- Don't repeat yourself! Let Ruby do the copy and paste for you.
- You can have "generic" types (Pokemon) and more specific types that use the things from the generic (WaterType)
- a battle between two different creatures
- creatures (pokemon)
- types (fire, water, air, shock, ground)
- names
- trainers (owners)
- attack!
- owners (trainers)
- name
- many pokemon (creatures)
- favourite pokemon
- age
Description:
- two pokemon exist
- attack each other a bunch
- some health each
- take turns fighting each other (by picking an attack)