Created
July 4, 2013 01:04
-
-
Save zeevallin/5924151 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal | |
attr_accessor :name | |
def self.named(name) | |
self.new(name) | |
end | |
def initialize(name) | |
self.name = name | |
end | |
def make_noise | |
puts "#{name} says #{sound}" | |
end | |
def sound | |
"" | |
end | |
end | |
class Kitten < Animal | |
def sound | |
"meow!" | |
end | |
end | |
class Human < Animal | |
def sound | |
"ouch!" | |
end | |
end | |
class AnimalCannon | |
attr_accessor :target, :ammunition | |
def self.load(target: nil, ammunition: nil) | |
self.new(target,ammunition) | |
end | |
def initialize(target,ammunition) | |
@target = target | |
@ammunition = ammunition | |
end | |
def fire | |
puts "Cannon fires the #{ammunition.class.to_s.downcase} named #{ammunition.name}." | |
ammunition.make_noise | |
sleep 3 | |
target.make_noise | |
end | |
end | |
pool_of_kittens = [ | |
Kitten.named("Bill"), | |
Kitten.named("Bull"), | |
Kitten.named("Findus"), | |
Kitten.named("Pelle") | |
] | |
available_targets = [ | |
Human.named("Zeeraw"), | |
Human.named("Cordite") | |
] | |
2.times do | |
AnimalCannon.load( | |
target: available_targets.sample, | |
ammunition: pool_of_kittens.sample | |
).fire | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment