Skip to content

Instantly share code, notes, and snippets.

@thoughtless
Forked from anonymous/gist:8deaba7f10190285bae5
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoughtless/fe87ac43ee52f061ef74 to your computer and use it in GitHub Desktop.
Save thoughtless/fe87ac43ee52f061ef74 to your computer and use it in GitHub Desktop.
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
(1..@data.length).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
@thoughtless
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment