Skip to content

Instantly share code, notes, and snippets.

@superacidjax
Created October 6, 2012 15:01
Show Gist options
  • Select an option

  • Save superacidjax/3845139 to your computer and use it in GitHub Desktop.

Select an option

Save superacidjax/3845139 to your computer and use it in GitHub Desktop.
class Person
attr_accessor :name, :gender, :pets
def initialize(n, g)
@name = n
@gender = g
@pets = {}
end
def to_s
"#{name} is a #{gender} and has #{pets.count} pets : #{pets}."
end
end
class Toy
attr_accessor :name, :cost
def initialize(n, c)
@name = n
@cost = c
end
def to_s
"This is a #{name} toy that cost $#{cost}.00"
end
end
class Animal
attr_accessor :name, :age, :species, :toys
def initialize(n, a, s)
@name = n
@age = a
@species = s
@toys = []
end
def to_s
"#{name} is a #{age} year old #{species} and has #{toys.count} toys : #{toys}"
end
end
puts "Animal (a) or Person (p) or Toy (t) or Quit (q)"
response = gets.chomp
people = {}
while response != 'q'
if response == 'p'
puts "Name?"
name = gets.chomp
puts "Gender?"
gender = gets.chomp
person = Person.new(name, gender)
people[name] = person
elsif response == 'a'
puts "Name?"
name = gets.chomp
puts "Age?"
age = gets.chomp
puts "Species?"
species = gets.chomp
animal = Animal.new(name, age, species)
puts "Pick from list of owners #{people.keys}"
owner = gets.chomp
people[owner].pets[name] = animal
else
puts "Name?"
name = gets.chomp
puts "Cost?"
cost = gets.chomp
toy = Toy.new(name, cost)
people.keys.each do |person_name|
puts "Here are #{person_name}'s animals"
people[person_name].pets.keys.each do |pet_name|
puts "Animal: #{pet_name}"
end
end
puts "Which owner?"
owner = gets.chomp
puts "Which animal?"
animal = gets.chomp
people[owner].pets[animal].toys << toy
end
puts "Animal (a) or Person (p) or Toy (t) or Quit (q)"
response = gets.chomp
end
people.keys.each do |name|
puts people[name]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment