Skip to content

Instantly share code, notes, and snippets.

@tealtail
Created September 11, 2013 14:18
Show Gist options
  • Save tealtail/6524271 to your computer and use it in GitHub Desktop.
Save tealtail/6524271 to your computer and use it in GitHub Desktop.
# Define Animal as a class
class Animal
# Set up accessors and mutators for the attributes of an Animal
# attr_accessor sets up both for you
attr_accessor :name, :age, :gender, :species, :toys
# Used when creating a new animal.
# Example:
# Animal.new('Timmy', 4, 'male', 'toad')
def initialize(name, age, gender, species)
@name = name
@age = age
@gender = gender
@species = species
@toys = []
end
# When we display the animal using puts or print, the
# to_s method is called to pretty print an Animal
def to_s
"#{@name} is a #{@age} year old #{@gender} #{@species} that loves #{@toys.join(', ')}"
end
end
class Shelter
attr_accessor :name, :address, :animals
def initialize(name, address)
@name = name
@address = address
@animals = {}
end
def display_animals
@animals.values.join("\n")
end
def to_s
"#{@name} shelter at #{@address} has #{@animals.count} animals."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment