Created
October 4, 2018 18:02
-
-
Save sevperez/2ec4a8cf72b42f20a20ac83f36ab10bb to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Laboratory | |
attr_accessor :scientists | |
def initialize(scientists, experiments) | |
@scientists = scientists | |
@experiments = experiments | |
end | |
def run_all_experiments | |
@scientists.each do |scientist| | |
@experiments.each do |experiment| | |
scientist.run_experiment(experiment) | |
end | |
end | |
end | |
end | |
class Experiment | |
attr_accessor :title | |
def initialize(title) | |
@title = title | |
end | |
end | |
class Scientist | |
attr_accessor :name | |
def initialize(name) | |
@name = name | |
end | |
def run_experiment(experiment) | |
puts "#{name} is now running the #{experiment.title} experiment." | |
end | |
end | |
class MadScientist < Scientist | |
def run_experiment(experiment) | |
if sabotage? | |
puts "#{name} is now sabotaging the #{experiment.title} experiment!" | |
else | |
puts "#{name} is now running the #{experiment.title} experiment." | |
end | |
end | |
private | |
def sabotage? | |
[true, false].sample | |
end | |
end | |
chemistry_experiment = Experiment.new("chemistry") | |
physics_experiment = Experiment.new("physics") | |
biology_experiment = Experiment.new("biology") | |
experiments = [chemistry_experiment, physics_experiment, biology_experiment] | |
marie_curie = Scientist.new("Marie Curie") | |
niels_bohr = Scientist.new("Niels Bohr") | |
hubert_farnsworth = MadScientist.new("Hubert Farnsworth") | |
scientists = [marie_curie, niels_bohr, hubert_farnsworth] | |
lab = Laboratory.new(scientists, experiments) | |
lab.run_all_experiments |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment