Skip to content

Instantly share code, notes, and snippets.

@NimaBoscarino
Created May 13, 2019 23:31
Show Gist options
  • Save NimaBoscarino/94bf7790ab6022843737087fe06f1f6e to your computer and use it in GitHub Desktop.
Save NimaBoscarino/94bf7790ab6022843737087fe06f1f6e to your computer and use it in GitHub Desktop.

W7 D1 Breakout

Today's Code Another version of Today's Code

Goal:

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)

POKÉMON

  • 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)
# Pokemon
# => species
# => nickname
# => other stuff...
class Pokemon
attr_reader :species
attr_accessor :nickname, :health
def initialize species, nickname
@species = species
@nickname = nickname
@health = 25
end
def sayWhatUp
puts @nickname
'What up'
end
# def nickname # getter
# @nickname
# end
# def nickname= newName # writer/setter
# @nickname = newName
# end
def attack enemy
puts '** Attack! **'
enemy.health = enemy.health - Random.rand(7)
end
def status
@nickname + '\'s health is ' + @health.to_s
end
end
class WaterType < Pokemon
def attack enemy
puts 'Splash!'
super enemy
end
end
class GroundType < Pokemon
def attack enemy
puts 'Rumble!'
super enemy
end
end
class FireType < Pokemon
def attack enemy
puts 'Flame on!'
super enemy
end
end
class ElectricType < Pokemon
def attack enemy
puts 'Zap!!!! ⚡️'
# also call the Pokemon.attack method, aka the parent's attack method
super enemy
end
end
lil_zap = ElectricType.new 'Pikachu', 'Lil Zap'
bulbous_boi = GroundType.new 'Bulbasaur', 'Bulbous Boi'
puts lil_zap.nickname
puts bulbous_boi.nickname
lil_zap.nickname = 'Big Zap'
lil_zap.attack(bulbous_boi)
until lil_zap.health <= 0 || bulbous_boi.health <= 0
lil_zap.attack(bulbous_boi)
bulbous_boi.attack(lil_zap)
end
puts lil_zap.status
puts bulbous_boi.status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment