thisContext Ruby keywords alias and BEGIN begin break case class def defined? do else elsif END end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield
thisContext Ruby keywords alias and BEGIN begin break case class def defined? do else elsif END end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield
yield self end def if_false self end end class FalseClass def if_true self end def if_false yield self end end class NilClass def if_true self end def if_false yield self end end
yield self end def if_false self end end class FalseClass def if_true self end def if_false yield self end end class NilClass def if_true self end def if_false yield self end end * Shamelessly swiped from http://yehudakatz.com/2009/10/04/emulating-smalltalks-conditionals-in-ruby/
= ids.map {|id| Animal.find(id) || MissingAnimal.new} => [#<Animal: @name="pig">, #<MissingAnimal:>, #<Animal: @name="sheep">] animals.each {|animal| if animal.is_a?(NilClass) puts 'no animal' else puts animal.name end } but no longer own the behavior
built.' 'This is the malt that lay in the house that Jack built.' 'This is the rat that ate the malt that lay in the house that Jack built.' 'This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.' 12
the hound and the horn that belonged to', 'the farmer sowing his corn that kept', 'the rooster that crowed in the morn that woke', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the maiden all forlorn that milked', 'the cow with the crumpled horn that tossed', 'the dog that worried', 'the cat that killed', 'the rat that ate', 'the malt that lay in', 'the house that Jack built' ]
[ 'the horse and the hound and the horn that belonged to', 'the farmer sowing his corn that kept', 'the rooster that crowed in the morn that woke', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the maiden all forlorn that milked', 'the cow with the crumpled horn that tossed', 'the dog that worried', 'the cat that killed', 'the rat that ate', 'the malt that lay in', 'the house that Jack built'] end
[ 'the horse and the hound and the horn that belonged to', 'the farmer sowing his corn that kept', 'the rooster that crowed in the morn that woke', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the maiden all forlorn that milked', 'the cow with the crumpled horn that tossed', 'the dog that worried', 'the cat that killed', 'the rat that ate', 'the malt that lay in', 'the house that Jack built'] end
data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', 'the farmer sowing his corn that kept', 'the rooster that crowed in the morn that woke', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the maiden all forlorn that milked', 'the cow with the crumpled horn that tossed', 'the dog that worried', 'the cat that killed', 'the rat that ate', 'the malt that lay in', 'the house that Jack built'] end
data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', 'the farmer sowing his corn that kept', 'the rooster that crowed in the morn that woke', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the maiden all forlorn that milked', 'the cow with the crumpled horn that tossed', 'the dog that worried', 'the cat that killed', 'the rat that ate', 'the malt that lay in', 'the house that Jack built'] end data.last(3) => ['the rat that ate', 'the malt that lay in', 'the house that Jack built']
data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end
data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end
data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end
"This is #{phrase(number)}.\n" end def phrase(number) data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end
"This is #{phrase(number)}.\n" end def phrase(number) data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end
(1..data.length).map {|i| line(i)}.join("\n") end def line(number) "This is #{phrase(number)}.\n" end def phrase(number) data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end
(1..data.length).map {|i| line(i)}.join("\n") end def line(number) "This is #{phrase(number)}.\n" end def phrase(number) data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end
(1..data.length).map {|i| line(i)}.join("\n") end def line(number) "This is #{phrase(number)}.\n" end def phrase(number) data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end
that Jack built.' puts House.new.line(2) => 'This is the malt that lay in the house that Jack built.' puts House.new.line(3) => 'This is the rat that ate the malt that lay in the house that Jack built.'
that Jack built.' puts House.new.line(2) => 'This is the malt that lay in the house that Jack built.' puts House.new.line(3) => 'This is the rat that ate the malt that lay in the house that Jack built.' puts House.new.line(12) => 'This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'
that Jack built.' puts House.new.line(2) => 'This is the malt that lay in the house that Jack built.' puts House.new.line(3) => 'This is the rat that ate the malt that lay in the house that Jack built.' puts House.new.line(12) => 'This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.' puts House.new.recite => 'This is the house that Jack built.' 'This is the malt that lay in the house that Jack built.' 'This is the rat that ate the malt that lay in the house that Jack built.' etc
the hound and the horn that belonged to', 'the farmer sowing his corn that kept', 'the rooster that crowed in the morn that woke', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the maiden all forlorn that milked', 'the cow with the crumpled horn that tossed', 'the dog that worried', 'the cat that killed', 'the rat that ate', 'the malt that lay in', 'the house that Jack built' ]
crowed in the morn that woke', 'the farmer sowing his corn that kept', 'the dog that worried', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the horse and the hound and the horn that belonged to', 'the malt that lay in', 'the cow with the crumpled horn that tossed', 'the house that Jack built', 'the cat that killed', 'the maiden all forlorn that milked', 'the rat that ate']
crowed in the morn that woke', 'the farmer sowing his corn that kept', 'the dog that worried', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the horse and the hound and the horn that belonged to', 'the malt that lay in', 'the cow with the crumpled horn that tossed', 'the house that Jack built', 'the cat that killed', 'the maiden all forlorn that milked', 'the rat that ate']
crowed in the morn that woke', 'the farmer sowing his corn that kept', 'the dog that worried', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the horse and the hound and the horn that belonged to', 'the malt that lay in', 'the cow with the crumpled horn that tossed', 'the house that Jack built', 'the cat that killed', 'the maiden all forlorn that milked', 'the rat that ate']
crowed in the morn that woke', 'the farmer sowing his corn that kept', 'the dog that worried', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the horse and the hound and the horn that belonged to', 'the malt that lay in', 'the cow with the crumpled horn that tossed', 'the house that Jack built', 'the cat that killed', 'the maiden all forlorn that milked', 'the rat that ate']
'This is the maiden all forlorn that milked the rat that ate.' 'This is the cat that killed the maiden all forlorn that milked the rat that ate.' 1 2 3
'This is the maiden all forlorn that milked the rat that ate.' 'This is the cat that killed the maiden all forlorn that milked the rat that ate.' 'This is the rooster that crowed in the morn that woke the farmer sowing his corn that kept the dog that worried the priest all shaven and shorn that married the man all tattered and torn that kissed the horse and the hound and the horn that belonged to the malt that lay in the cow with the crumpled horn that tossed the house that Jack built the cat that killed the maiden all forlorn that milked the rat that ate.' 12
'This is the maiden all forlorn that milked the rat that ate.' 'This is the cat that killed the maiden all forlorn that milked the rat that ate.' 'This is the rooster that crowed in the morn that woke the farmer sowing his corn that kept the dog that worried the priest all shaven and shorn that married the man all tattered and torn that kissed the horse and the hound and the horn that belonged to the malt that lay in the cow with the crumpled horn that tossed the house that Jack built the cat that killed the maiden all forlorn that milked the rat that ate.' 12
def line(number) def phrase(number) def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end
def line(number) def phrase(number) def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end Task: Implement RandomHouse without 'if' statements
def line(number) def phrase(number) def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end Inheritance?
def line(number) def phrase(number) def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end class RandomHouse < House def data @data ||= super.shuffle end end
def line(number) def phrase(number) def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end class RandomHouse < House def data @data ||= super.shuffle end end
def line(number) def phrase(number) def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end class RandomHouse < House def data @data ||= super.shuffle end end
def line(number) def phrase(number) def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end class RandomHouse < House def data @data ||= super.shuffle end end
def line(number) def phrase(number) def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end class RandomHouse < House def data @data ||= super.shuffle end end
is the rat that ate.' puts randomhouse.line(2) => 'This is the maiden all forlorn that milked the rat that ate.' puts randomhouse.line(3) => 'This is the cat that killed the maiden all forlorn that milked the rat that ate.'
is the rat that ate.' puts randomhouse.line(2) => 'This is the maiden all forlorn that milked the rat that ate.' puts randomhouse.line(3) => 'This is the cat that killed the maiden all forlorn that milked the rat that ate.' puts randomhouse.line(12) => 'This is the rooster that crowed in the morn that woke the farmer sowing his corn that kept the dog that worried the priest all shaven and shorn that married the man all tattered and torn that kissed the horse and the hound and the horn that belonged to the malt that lay in the cow with the crumpled horn that tossed the house that Jack built the cat that killed the maiden all forlorn that milked the rat that ate.'
is the rat that ate.' puts randomhouse.line(2) => 'This is the maiden all forlorn that milked the rat that ate.' puts randomhouse.line(3) => 'This is the cat that killed the maiden all forlorn that milked the rat that ate.' puts randomhouse.line(12) => 'This is the rooster that crowed in the morn that woke the farmer sowing his corn that kept the dog that worried the priest all shaven and shorn that married the man all tattered and torn that kissed the horse and the hound and the horn that belonged to the malt that lay in the cow with the crumpled horn that tossed the house that Jack built the cat that killed the maiden all forlorn that milked the rat that ate.'
is the rat that ate.' puts randomhouse.line(2) => 'This is the maiden all forlorn that milked the rat that ate.' puts randomhouse.line(3) => 'This is the cat that killed the maiden all forlorn that milked the rat that ate.' puts randomhouse.line(12) => 'This is the rooster that crowed in the morn that woke the farmer sowing his corn that kept the dog that worried the priest all shaven and shorn that married the man all tattered and torn that kissed the horse and the hound and the horn that belonged to the malt that lay in the cow with the crumpled horn that tossed the house that Jack built the cat that killed the maiden all forlorn that milked the rat that ate.'
is the rat that ate.' puts randomhouse.line(2) => 'This is the maiden all forlorn that milked the rat that ate.' puts randomhouse.line(3) => 'This is the cat that killed the maiden all forlorn that milked the rat that ate.' puts randomhouse.line(12) => 'This is the rooster that crowed in the morn that woke the farmer sowing his corn that kept the dog that worried the priest all shaven and shorn that married the man all tattered and torn that kissed the horse and the hound and the horn that belonged to the malt that lay in the cow with the crumpled horn that tossed the house that Jack built the cat that killed the maiden all forlorn that milked the rat that ate.'
the house that Jack built. This is the malt that lay in the malt that lay in the house that Jack built the house that Jack built. This is the rat that ate the rat that ate the malt that lay in the malt that lay in the house that Jack built the house that Jack built.
(1..data.length).map {|i| line(i)}.join("\n") end def line(number) "This is #{phrase(number)}.\n" end def phrase(number) data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end
(1..data.length).map {|i| line(i)}.join("\n") end def line(number) "This is #{phrase(number)}.\n" end def phrase(number) data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end
def line(number) def phrase(number) data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end
def line(number) def phrase(number) data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end
def line(number) def phrase(number) data.last(number).join(" ") end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end
def line(number) def phrase(number) data.last(number).join(" ") end def parts(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end
def line(number) def phrase(number) data.last(number).join(" ") end def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end
def line(number) def phrase(number) parts(number).join(" ") end def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end
def line(number) def phrase(number) parts(number).join(" ") end def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end
def line(number) def phrase(number) def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end
def line(number) def phrase(number) def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end ['the rat that ate', 'the malt that lay in', 'the house that Jack built'] have
def line(number) def phrase(number) def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end ['the rat that ate', 'the malt that lay in', 'the house that Jack built'] ['the rat that ate', 'the rat that ate', 'the malt that lay in', 'the malt that lay in', 'the house that Jack built', 'the house that Jack built'] want
def line(number) def phrase(number) def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end
def line(number) def phrase(number) def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end Task: Implement EchoHouse without 'if' statements
def line(number) def phrase(number) def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end Inheritance?
def line(number) def phrase(number) def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end class EchoHouse < House def parts(number) super.zip(super).flatten end end
def line(number) def phrase(number) def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end class EchoHouse < House def parts(number) super.zip(super).flatten end end
def line(number) def phrase(number) def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end class EchoHouse < House def parts(number) super.zip(super).flatten end end
def line(number) def phrase(number) def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end class EchoHouse < House def parts(number) super.zip(super).flatten end end
def line(number) def phrase(number) def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end class EchoHouse < House def parts(number) super.zip(super).flatten end end
that lay in', 'the house that Jack built'] ['the rat that ate', 'the malt that lay in', 'the house that Jack built'] [['the rat that ate','the rat that ate'], ['the malt that lay in','the malt that lay in'], ['the house that Jack built','the house that Jack built']] super.zip(super)
that lay in', 'the house that Jack built'] super.zip(super).flatten ['the rat that ate', 'the malt that lay in', 'the house that Jack built'] [['the rat that ate','the rat that ate'], ['the malt that lay in','the malt that lay in'], ['the house that Jack built','the house that Jack built']] super.zip(super)
that lay in', 'the house that Jack built'] super.zip(super).flatten ['the rat that ate', 'the malt that lay in', 'the house that Jack built'] [['the rat that ate','the rat that ate'], ['the malt that lay in','the malt that lay in'], ['the house that Jack built','the house that Jack built']] super.zip(super) ['the rat that ate','the rat that ate', 'the malt that lay in','the malt that lay in', 'the house that Jack built','the house that Jack built']
def line(number) def phrase(number) def parts(number) data.last(number) end def data [ 'the horse and the hound and the horn that belonged to', # ... 'the malt that lay in', 'the house that Jack built'] end end class EchoHouse < House def parts(number) super.zip(super).flatten end end
Jack built the house that Jack built.' puts EchoHouse.new.line(2) 'This is the malt that lay in the malt that lay in the house that Jack built the house that Jack built.'
Jack built the house that Jack built.' puts EchoHouse.new.line(2) 'This is the malt that lay in the malt that lay in the house that Jack built the house that Jack built.' puts EchoHouse.new.line(3) 'This is the rat that ate the rat that ate the malt that lay in the malt that lay in the house that Jack built the house that Jack built.'
Jack built the house that Jack built.' puts EchoHouse.new.line(2) 'This is the malt that lay in the malt that lay in the house that Jack built the house that Jack built.' puts EchoHouse.new.line(3) 'This is the rat that ate the rat that ate the malt that lay in the malt that lay in the house that Jack built the house that Jack built.' puts EchoHouse.new.line(12) 'This is the horse and the hound and the horn that belonged to the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the farmer sowing his corn that kept the rooster that crowed in the morn that woke the rooster that crowed in the morn that woke the priest all shaven and shorn that married the priest all shaven and shorn that married the man all tattered and torn that kissed the man all tattered and torn that kissed the maiden all forlorn that milked the maiden all forlorn that milked the cow with the crumpled horn that tossed the cow with the crumpled horn that tossed the dog that worried the dog that worried the cat that killed the cat that killed the rat that ate the rat that ate the malt that lay in the malt that lay in the house that Jack built the house that Jack built.'
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end class House def recite def line(number) def phrase(number) def parts(number) def data end
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end class House def recite def line(number) def phrase(number) def parts(number) def data end
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end class House def recite def line(number) def phrase(number) def parts(number) def data end What could possibly go wrong?
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end class House def recite def line(number) def phrase(number) def parts(number) def data end
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end RandomEchoHouse < RandomHouse < EchoHouse data parts
data @data ||= super.shuffle end end class RandomEchoHouse < RandomHouse def parts(number) super.zip(super).flatten end end class EchoHouse < House def parts(number) super.zip(super).flatten end end RandomEchoHouse < RandomHouse < EchoHouse data parts
data @data ||= super.shuffle end end class RandomEchoHouse < RandomHouse def parts(number) super.zip(super).flatten end end class EchoHouse < House def parts(number) super.zip(super).flatten end end RandomEchoHouse < RandomHouse < EchoHouse data parts
data @data ||= super.shuffle end end class RandomEchoHouse < RandomHouse def parts(number) super.zip(super).flatten end end class EchoHouse < House def parts(number) super.zip(super).flatten end end RandomEchoHouse < RandomHouse < EchoHouse Inherit data parts
data @data ||= super.shuffle end end class RandomEchoHouse < RandomHouse def parts(number) super.zip(super).flatten end end class EchoHouse < House def parts(number) super.zip(super).flatten end end RandomEchoHouse < RandomHouse < EchoHouse Inherit data Duplicate parts
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end RandomEchoHouse < RandomHouse < EchoHouse Inherit data Duplicate parts
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end class RandomEchoHouse < EchoHouse def data @data ||= super.shuffle end end RandomEchoHouse < RandomHouse < EchoHouse Inherit data Duplicate parts
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end class RandomEchoHouse < EchoHouse def data @data ||= super.shuffle end end RandomEchoHouse < RandomHouse < EchoHouse Inherit data Duplicate parts
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end class RandomEchoHouse < EchoHouse def data @data ||= super.shuffle end end RandomEchoHouse < RandomHouse < EchoHouse Inherit data Duplicate parts Inherit
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end class RandomEchoHouse < EchoHouse def data @data ||= super.shuffle end end RandomEchoHouse < RandomHouse < EchoHouse Inherit data Duplicate Duplicate parts Inherit
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end class RandomEchoHouse < House def data @data ||= super.shuffle end def parts(number) super.zip(super).flatten end end
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end class RandomEchoHouse < House def data @data ||= super.shuffle end def parts(number) super.zip(super).flatten end end
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end class RandomEchoHouse < House def data @data ||= super.shuffle end def parts(number) super.zip(super).flatten end end
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end class RandomEchoHouse < House def data @data ||= super.shuffle end def parts(number) super.zip(super).flatten end end
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end class RandomEchoHouse < House def data @data ||= super.shuffle end def parts(number) super.zip(super).flatten end end
[ 'the horse and the hound and the horn that belonged to', 'the farmer sowing his corn that kept', 'the rooster that crowed in the morn that woke', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the maiden all forlorn that milked', 'the cow with the crumpled horn that tossed', 'the dog that worried', 'the cat that killed', 'the rat that ate', 'the malt that lay in', 'the house that Jack built'] end end class RandomHouse < House def data @data ||= super.shuffle end end
[ 'the horse and the hound and the horn that belonged to', 'the farmer sowing his corn that kept', 'the rooster that crowed in the morn that woke', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the maiden all forlorn that milked', 'the cow with the crumpled horn that tossed', 'the dog that worried', 'the cat that killed', 'the rat that ate', 'the malt that lay in', 'the house that Jack built'] end end class RandomHouse < House def data @data ||= super.shuffle end end What does RandomHouse change?
[ 'the horse and the hound and the horn that belonged to', 'the farmer sowing his corn that kept', 'the rooster that crowed in the morn that woke', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the maiden all forlorn that milked', 'the cow with the crumpled horn that tossed', 'the dog that worried', 'the cat that killed', 'the rat that ate', 'the malt that lay in', 'the house that Jack built'] end end class RandomHouse < House def data @data ||= super.shuffle end end
[ 'the horse and the hound and the horn that belonged to', 'the farmer sowing his corn that kept', 'the rooster that crowed in the morn that woke', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the maiden all forlorn that milked', 'the cow with the crumpled horn that tossed', 'the dog that worried', 'the cat that killed', 'the rat that ate', 'the malt that lay in', 'the house that Jack built'] end end class RandomHouse < House def data @data ||= super.shuffle end end Make this
[ 'the horse and the hound and the horn that belonged to', 'the farmer sowing his corn that kept', 'the rooster that crowed in the morn that woke', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the maiden all forlorn that milked', 'the cow with the crumpled horn that tossed', 'the dog that worried', 'the cat that killed', 'the rat that ate', 'the malt that lay in', 'the house that Jack built'] end end class RandomHouse < House def data @data ||= super.shuffle end end Make this More like this
[ 'the horse and the hound and the horn that belonged to', 'the farmer sowing his corn that kept', 'the rooster that crowed in the morn that woke', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the maiden all forlorn that milked', 'the cow with the crumpled horn that tossed', 'the dog that worried', 'the cat that killed', 'the rat that ate', 'the malt that lay in', 'the house that Jack built'] end end
[ 'the horse and the hound and the horn that belonged to', 'the farmer sowing his corn that kept', 'the rooster that crowed in the morn that woke', 'the priest all shaven and shorn that married', 'the man all tattered and torn that kissed', 'the maiden all forlorn that milked', 'the cow with the crumpled horn that tossed', 'the dog that worried', 'the cat that killed', 'the rat that ate', 'the malt that lay in', 'the house that Jack built'] end
[ 'the horse and the hound and the horn that belonged to', # ... 'the house that Jack built'] def data DATA end end class RandomHouse < House def data @data ||= super.shuffle end end
[ 'the horse and the hound and the horn that belonged to', # ... 'the house that Jack built'] def data @data ||= DATA end end class RandomHouse < House def data @data ||= super.shuffle end end
[ 'the horse and the hound and the horn that belonged to', # ... 'the house that Jack built'] def data @data ||= DATA end end class RandomHouse < House def data @data ||= DATA.shuffle end end
[ 'the horse and the hound and the horn that belonged to', # ... 'the house that Jack built'] def data @data ||= DATA end end class RandomHouse < House def data @data ||= DATA.shuffle end end What changed?
[ 'the horse and the hound and the horn that belonged to', # ... 'the house that Jack built'] def data DATA end end class RandomHouse < House def data @data ||= super.shuffle end end
[ 'the horse and the hound and the horn that belonged to', # ... 'the house that Jack built'] attr_reader :data def initialize @data = DATA end def data DATA end # ... end
[ 'the horse and the hound and the horn that belonged to', # ... 'the house that Jack built'] attr_reader :data def initialize @data = DATA end # ... end
[ 'the horse and the hound and the horn that belonged to', # ... 'the house that Jack built'] attr_reader :data def initialize @data = DATA end # ... end
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new.line(12)
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new.line(12) => 'This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new.line(12) => 'This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new.line(12) => 'This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.' More code
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new.line(12) => 'This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.' More code Same behavior
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new.line(12) => 'This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.' More code Same behavior Awesome
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new.line(12) => 'This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new.line(12) class RandomOrder def order(data) data.shuffle end end
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new( ).line(12) class RandomOrder def order(data) data.shuffle end end
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new(orderer: RandomOrder.new).line(12) class RandomOrder def order(data) data.shuffle end end
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new(orderer: RandomOrder.new).line(12) => 'This is the rooster that crowed in the morn that woke the farmer sowing his corn that kept the dog that worried the priest all shaven and shorn that married the man all tattered and torn that kissed the horse and the hound and the horn that belonged to the malt that lay in the cow with the crumpled horn that tossed the house that Jack built the cat that killed the maiden all forlorn that milked the rat that ate.' class RandomOrder def order(data) data.shuffle end end
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new(orderer: RandomOrder.new).line(12) => 'This is the rooster that crowed in the morn that woke the farmer sowing his corn that kept the dog that worried the priest all shaven and shorn that married the man all tattered and torn that kissed the horse and the hound and the horn that belonged to the malt that lay in the cow with the crumpled horn that tossed the house that Jack built the cat that killed the maiden all forlorn that milked the rat that ate.' class RandomOrder def order(data) data.shuffle end end
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new(orderer: RandomOrder.new).line(12) => 'This is the rooster that crowed in the morn that woke the farmer sowing his corn that kept the dog that worried the priest all shaven and shorn that married the man all tattered and torn that kissed the horse and the hound and the horn that belonged to the malt that lay in the cow with the crumpled horn that tossed the house that Jack built the cat that killed the maiden all forlorn that milked the rat that ate.' class RandomOrder def order(data) data.shuffle end end
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new(orderer: RandomOrder.new).line(12) => 'This is the rooster that crowed in the morn that woke the farmer sowing his corn that kept the dog that worried the priest all shaven and shorn that married the man all tattered and torn that kissed the horse and the hound and the horn that belonged to the malt that lay in the cow with the crumpled horn that tossed the house that Jack built the cat that killed the maiden all forlorn that milked the rat that ate.' class RandomOrder def order(data) data.shuffle end end
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new(orderer: RandomOrder.new).line(12) => 'This is the rooster that crowed in the morn that woke the cat that killed the cow with the crumpled horn that tossed the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the priest all shaven and shorn that married the man all tattered and torn that kissed the house that Jack built the malt that lay in the rat that ate the maiden all forlorn that milked the dog that worried.' class RandomOrder def order(data) data.shuffle end end Composition
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new(orderer: RandomOrder.new).line(12) => 'This is the rooster that crowed in the morn that woke the cat that killed the cow with the crumpled horn that tossed the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the priest all shaven and shorn that married the man all tattered and torn that kissed the house that Jack built the malt that lay in the rat that ate the maiden all forlorn that milked the dog that worried.' class RandomOrder def order(data) data.shuffle end end Inject an object to play the role of the thing that varies
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @data = orderer.order(DATA) end # ... end class DefaultFormatter def format(parts) parts end end
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @data = orderer.order(DATA) end # ... end class DefaultFormatter def format(parts) parts end end
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end # ... end class DefaultFormatter def format(parts) parts end end
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end # ... end class DefaultFormatter def format(parts) parts end end
:data def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end # ... end class DefaultFormatter def format(parts) parts end end
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end # ... end class DefaultFormatter def format(parts) parts end end
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end # ... end class DefaultFormatter def format(parts) parts end end
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end def parts(number) data.last(number) end # ... end class DefaultFormatter def format(parts) parts end end
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end def parts(number) data.last(number) end # ... end class DefaultFormatter def format(parts) parts end end
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end def parts(number) formatter.format(data.last(number)) end # ... end class DefaultFormatter def format(parts) parts end end
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end # ... end class DefaultFormatter def format(parts) parts end end
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end # ... end class DefaultFormatter def format(parts) parts end end
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end # ... end puts House.new.line(12) class DefaultFormatter def format(parts) parts end end
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end # ... end puts House.new.line(12) => 'This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.' class DefaultFormatter def format(parts) parts end end
parts end end class House attr_reader :formatter, :data def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end # ... end class EchoFormatter def format(parts) parts.zip(parts).flatten end end
parts end end class House attr_reader :formatter, :data def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end # ... end puts House.new(formatter: EchoFormatter.new).line(12) class EchoFormatter def format(parts) parts.zip(parts).flatten end end
parts end end class House attr_reader :formatter, :data def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @formatter = formatter @data = orderer.order(DATA) end # ... end puts House.new(formatter: EchoFormatter.new).line(12) => 'This is the horse and the hound and the horn that belonged to the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the farmer sowing his corn that kept the rooster that crowed in the morn that woke the rooster that crowed in the morn that woke the priest all shaven and shorn that married the priest all shaven and shorn that married the man all tattered and torn that kissed the man class EchoFormatter def format(parts) parts.zip(parts).flatten end end
parts end end class EchoFormatter def format(parts) parts.zip(parts).flatten end end class DefaultOrder def order(data) data end end class RandomOrder def order(data) data.shuffle end end Orderer Role Formatter Role