Skip to content

Commit

Permalink
zoo.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
oribrost committed May 24, 2012
1 parent e8ad8b4 commit b2eb8e2
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions zoo.rb
@@ -0,0 +1,55 @@
def sum(x)
x.reduce {|x,y| x + y}
end

class Animal
attr_reader :gender
def initialize(gender)
@gender = gender
end
end

class Zebra < Animal
def legs()
4
end
def initialize(gender)
super gender
end
end

class Snake < Animal
def legs()
0
end
def initialize(gender)
super gender
end
end

class Cage
attr_accessor :animals
def initialize(animals)
self.animals = animals
end
end

class Zoo
attr_accessor :cages
def initialize(cages)
self.cages = cages
end
def animal_count
sum(self.cages.map {|x| x.animals.length })
end
def cage_count
self.cages.length
end
end

z = Zoo.new([Cage.new([Zebra.new(:male), Zebra.new(:female), Zebra.new(:male)]),
Cage.new([Snake.new(:female), Snake.new(:male), Snake.new(:female), Snake.new(:female), Snake.new(:female)]),
Cage.new([Zebra.new(:male), Snake.new(:male)])])

puts z.animal_count.inspect
puts z.cage_count

0 comments on commit b2eb8e2

Please sign in to comment.