Skip to content

Instantly share code, notes, and snippets.

@amyhenning
Last active October 27, 2018 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amyhenning/2bd70245eadea6be55232214b63a6682 to your computer and use it in GitHub Desktop.
Save amyhenning/2bd70245eadea6be55232214b63a6682 to your computer and use it in GitHub Desktop.
class Pokemon
# from here until line 9, we are INSIDE the class
attr_accessor :name, :type, :level
# when a Pokemon is created, its name, type, and level must be set
def initialize(name, type, level)
@name = name
@type = type
@level = level
end
# You can call this method from outside the class.
# It is a proxy to allow you to read the Pokemon's state.
def pokedex
puts "#{@name} is a #{@type} type Pokemon. It is at level #{@level}."
end
# You can call this method from outside the class on a Pokemon object.
# It will update the state of the Pokemon object.
def level_up(n)
@level = n
end
end
# from here on out, we are OUTSIDE the class
pokemon = Pokemon.new("Charmander", "fire", 12) # create a new Pokemon instance
pokemon.pokedex # => "Charmander is a fire type Pokemon. It is at level 12."
pokemon.level_up(13) # updates Charmander's level
pokemon.pokedex # => "Charmander is a fire type Pokemon. It is at level 13."
puts pokemon.type # => "fire" (now it works, thanks to the attr_accessor on line 3!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment