-
-
Save thoughtless/fe87ac43ee52f061ef74 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
module DefaultOrder | |
module_function | |
def order(data) | |
data | |
end | |
end | |
module RandomOrder | |
module_function | |
def order(data) | |
data.shuffle | |
end | |
end | |
module DefaultFormatter | |
module_function | |
def format(parts) | |
parts | |
end | |
end | |
module EchoFormatter | |
module_function | |
def format(parts) | |
parts.zip(parts).flatten | |
end | |
end | |
class House | |
DATA =[ | |
"the cat that killed", | |
"the rat that ate", | |
"the malt that lay in", | |
"the house that Jack built" | |
] | |
def initialize | |
@data = orderer.order(DATA) | |
end | |
def recite | |
([email protected]).map {|i| line(i)}.join("\n") | |
end | |
def line(number) | |
"This is #{phrase(number)}.\n" | |
end | |
def phrase(number) | |
parts(number).join(" ") | |
end | |
def parts(number) | |
formatter.format(@data.last(number)) | |
end | |
def orderer | |
DefaultOrder | |
end | |
def formatter | |
DefaultFormatter | |
end | |
end | |
class RandomHouse < House | |
def orderer | |
RandomOrder | |
end | |
end | |
class EchoHouse < House | |
def formatter | |
EchoFormatter | |
end | |
end | |
class RandomEchoHouse < House | |
def orderer | |
RandomOrder | |
end | |
def formatter | |
EchoFormatter | |
end | |
end | |
puts House.new.recite | |
puts "="*88 | |
puts "="*88 | |
puts "="*88 | |
puts RandomHouse.new.recite | |
puts "="*88 | |
puts "="*88 | |
puts "="*88 | |
puts EchoHouse.new.recite | |
puts "="*88 | |
puts "="*88 | |
puts "="*88 | |
puts RandomEchoHouse.new.recite |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://www.youtube.com/watch?v=OMPfEXIlTVE for context.