Skip to content

Instantly share code, notes, and snippets.

@superacidjax
Created October 5, 2012 23:18
Show Gist options
  • Save superacidjax/3843019 to your computer and use it in GitHub Desktop.
Save superacidjax/3843019 to your computer and use it in GitHub Desktop.
Lab 2 Solution
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."
end
end
class Animal
attr_accessor :name, :age, :species
def initialize(n, a, s)
@name = n
@age = a
@species = s
end
end
puts "Animal (a) or Person (p) or Quit (q)"
response = gets.chomp
people = {}
while response == 'a' || response == 'p'
if response == 'p'
puts "Name?"
name = gets.chomp
puts "Gender?"
gender = gets.chomp
person = Person.new(name, gender)
people[name] = person
else
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 << animal
end
puts "Animal (a) or Person (p) 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