Share
Embed
Nothing is Something (RailsConf)
Our code is full of hidden assumptions, things that seem like nothing, secrets that we did not name and thus cannot see.
These secrets represent missing concepts and this talk shows you how to expose those concepts with code that is easy to understand, change and extend.
Being explicit about hidden ideas makes your code simpler, your apps clearer and your life better. Even very small ideas matter. Everything, even nothing, is something.
Sandi Metz
April 16, 2015
Transcript
518 slides
-
Jan 2015 @sandimetz 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 -
Jan 2015 @sandimetz 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 => 2 -
Jan 2015 @sandimetz 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 => 2 1.send(:+, 1) -
Jan 2015 @sandimetz 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 => 2 1.send(:+, 1) => 2 -
Jan 2015 @sandimetz 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 => 2 1.send(:+, 1) => 2 -
Jan 2015 @sandimetz 1.class 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 => 2 1.send(:+, 1) => 2 -
Jan 2015 @sandimetz 1.class => Fixnum 1.to_s =>
"1" 1.send(:to_s) => "1" 1 + 1 => 2 1.send(:+, 1) => 2 -
Jan 2015 @sandimetz Fixnum.instance_methods(false) => [ :to_s,
:+, :-‐, :*, :/, :==, :%, :modulo, # etc 1.to_s => "1" 1.send(:to_s) => "1" 1 + 1 => 2 1.send(:+, 1) => 2 -
Jan 2015 @sandimetz Fixnum.instance_methods(false) => [ :to_s,
:+, :-‐, :*, :/, :==, :%, :modulo, # etc 1.to_s => "1" 1.send(:to_s) => "1" 1 + 1 => 2 1.send(:+, 1) => 2 -
Jan 2015 @sandimetz Fixnum.instance_methods(false) => [ :to_s,
:+, :-‐, :*, :/, :==, :%, :modulo, # etc 1.to_s => "1" 1.send(:to_s) => "1" 1 + 1 => 2 1.send(:+, 1) => 2 -
Jan 2015 @sandimetz Fixnum.instance_methods(false) => [ :to_s,
:+, :-‐, :*, :/, :==, :%, :modulo, # etc 1.to_s => "1" 1.send(:to_s) => "1" 1 + 1 => 2 1.send(:+, 1) => 2 -
Jan 2015 @sandimetz 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 => 2 1.send(:+, 1) => 2 1 == 1 -
Jan 2015 @sandimetz 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 => 2 1.send(:+, 1) => 2 1 == 1 => true -
Jan 2015 @sandimetz 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 => 2 1.send(:+, 1) => 2 1 == 1 => true -
Jan 2015 @sandimetz 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 => 2 1.send(:+, 1) => 2 1 == 1 => true true.class -
Jan 2015 @sandimetz 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 => 2 1.send(:+, 1) => 2 1 == 1 => true true.class => TrueClass -
Jan 2015 @sandimetz 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 => 2 1.send(:+, 1) => 2 1 == 1 => true true.class => TrueClass -
Jan 2015 @sandimetz 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 => 2 1.send(:+, 1) => 2 1 == 1 => true true.class => TrueClass TrueClass.instance_methods(false) => [ :to_s, :inspect, :&, :|, :^, :pretty_print_cycle, :pretty_print] -
Jan 2015 @sandimetz 1.to_s => "1" 1.send(:to_s)
=> "1" 1 + 1 => 2 1.send(:+, 1) => 2 1 == 1 => true true.class => TrueClass TrueClass.instance_methods(false) => [ :to_s, :inspect, :&, :|, :^, :pretty_print_cycle, :pretty_print] -
Jan 2015 @sandimetz Smalltalk keywords true, false, nil, self, super,
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 -
Jan 2015 @sandimetz Smalltalk keywords true, false, nil, self, super,
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 -
Jan 2015 @sandimetz if (1 == 1) 'is
true' else 'is false' end -
Jan 2015 @sandimetz if (1 == 1) 'is
true' else 'is false' end true -
Jan 2015 @sandimetz if (1 == 1) 'is
true' else 'is false' end => 'is true' true -
Jan 2015 @sandimetz if (1 == 1) code
to eval when ‘true’ else code to eval when ‘false’ end true -
Jan 2015 @sandimetz if ( truthy ) code
to eval when ‘truthy’ else code to eval when ‘falsey’ end -
Jan 2015 @sandimetz if (some object whose type I know)
code to do some stuff else code to do some other stuff end -
Jan 2015 @sandimetz I just want to send a message
to an object -
Jan 2015 @sandimetz class TrueClass def if_true
yield end end -
Jan 2015 @sandimetz class TrueClass def if_true
yield self end end -
Jan 2015 @sandimetz class TrueClass def if_true
yield self end def if_false self end end -
Jan 2015 @sandimetz class TrueClass def if_true
yield self end def if_false self end end class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz class TrueClass def if_true
yield self end def if_false self end end class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz class TrueClass def if_true
yield self end def if_false self end end class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz class TrueClass def if_true
yield self end def if_false self end end class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz class TrueClass def if_true
yield self end def if_false self end end class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz class TrueClass def if_true
yield self end def if_false self end end class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz class TrueClass def if_true
yield self end def if_false self end end class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"} class
TrueClass def if_true yield self end # … end true -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"} class
TrueClass def if_true yield self end # … end -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"} class
TrueClass def if_true yield self end # … end -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"}
"evaluated block" class TrueClass def if_true yield self end # … end -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"}
"evaluated block" (1 == 1).if_false {puts "evaluated block"} class TrueClass def if_true yield self end # … end -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"}
"evaluated block" (1 == 1).if_false {puts "evaluated block"} class TrueClass def if_true yield self end def if_false self end end -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"}
"evaluated block" (1 == 1).if_false {puts "evaluated block"} # block is ignored class TrueClass def if_true yield self end def if_false self end end -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"}
"evaluated block" (1 == 1).if_false {puts "evaluated block"} # block is ignored (1 == 2).if_true {puts "evaluated block"} -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"}
"evaluated block" (1 == 1).if_false {puts "evaluated block"} # block is ignored (1 == 2).if_true {puts "evaluated block"} -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"}
"evaluated block" (1 == 1).if_false {puts "evaluated block"} # block is ignored (1 == 2).if_true {puts "evaluated block"} false -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"}
"evaluated block" (1 == 1).if_false {puts "evaluated block"} # block is ignored (1 == 2).if_true {puts "evaluated block"} false class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"}
"evaluated block" (1 == 1).if_false {puts "evaluated block"} # block is ignored (1 == 2).if_true {puts "evaluated block"} class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"}
"evaluated block" (1 == 1).if_false {puts "evaluated block"} # block is ignored (1 == 2).if_true {puts "evaluated block"} # block is ignored class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"}
"evaluated block" (1 == 1).if_false {puts "evaluated block"} # block is ignored (1 == 2).if_true {puts "evaluated block"} # block is ignored (1 == 2).if_false {puts "evaluated block"} class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz (1 == 1).if_true {puts "evaluated block"}
"evaluated block" (1 == 1).if_false {puts "evaluated block"} # block is ignored (1 == 2).if_true {puts "evaluated block"} # block is ignored (1 == 2).if_false {puts "evaluated block"} "evaluated block" class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz class TrueClass def if_true
yield self end def if_false self end end class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz class TrueClass def if_true
yield self end def if_false self end end class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz class Object def if_true
yield self end def if_false self end end class FalseClass def if_true self end def if_false yield self end end -
Jan 2015 @sandimetz class Object def if_true
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 -
Jan 2015 @sandimetz class Object def if_true
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/ -
Jan 2015 @sandimetz "anything".if_true {puts "evaluated block"} "evaluated
block" "anything".if_false {puts "evaluated block"} -
Jan 2015 @sandimetz "anything".if_true {puts "evaluated block"} "evaluated
block" "anything".if_false {puts "evaluated block"} # block is ignored -
Jan 2015 @sandimetz "anything".if_true {puts "evaluated block"} "evaluated
block" "anything".if_false {puts "evaluated block"} # block is ignored nil.if_true {puts "evaluated block"} -
Jan 2015 @sandimetz "anything".if_true {puts "evaluated block"} "evaluated
block" "anything".if_false {puts "evaluated block"} # block is ignored nil.if_true {puts "evaluated block"} # block is ignored -
Jan 2015 @sandimetz "anything".if_true {puts "evaluated block"} "evaluated
block" "anything".if_false {puts "evaluated block"} # block is ignored nil.if_true {puts "evaluated block"} # block is ignored nil.if_false {puts "evaluated block"} -
Jan 2015 @sandimetz "anything".if_true {puts "evaluated block"} "evaluated
block" "anything".if_false {puts "evaluated block"} # block is ignored nil.if_true {puts "evaluated block"} # block is ignored nil.if_false {puts "evaluated block"} "evaluated block" -
Jan 2015 @sandimetz if (1 == 1) puts
'is true' else puts 'is false' end => 'is true' -
Jan 2015 @sandimetz (1 == 1). if_true {puts
'is true'}. if_false {puts 'is false’} => 'is true' if (1 == 1) puts 'is true' else puts 'is false' end => 'is true' -
Jan 2015 @sandimetz if (1 == 2) puts
'is true' else puts 'is false' end => 'is false' (1 == 1). if_true {puts 'is true'}. if_false {puts 'is false’} => 'is true' if (1 == 1) puts 'is true' else puts 'is false' end => 'is true' -
Jan 2015 @sandimetz if (1 == 2) puts
'is true' else puts 'is false' end => 'is false' (1 == 1). if_true {puts 'is true'}. if_false {puts 'is false’} => 'is true' if (1 == 1) puts 'is true' else puts 'is false' end => 'is true' (1 == 2). if_true {puts 'is true'}. if_false {puts 'is false’} => 'is false' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.name} => 'pig' NoMethodError: undefined method `name' for nil:NilClass -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.name} => 'pig' NoMethodError: undefined method `name' for nil:NilClass -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.name} => 'pig' NoMethodError: undefined method `name' for nil:NilClass -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal: @name="pig">, nil, #<Animal: @name="sheep">] -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal: @name="pig">, nil, #<Animal: @name="sheep">] animals.compact => [#<Animal: @name="pig">, #<Animal: @name="sheep">] -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal: @name="pig">, nil, #<Animal: @name="sheep">] animals.compact => [#<Animal: @name="pig">, #<Animal: @name="sheep">] animals.each {|animal|puts animal.name} => 'pig' 'sheep' -
Jan 2015 @sandimetz But if you send it a message
nil is something -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.name} => 'pig' NoMethodError: undefined method `name' for nil:NilClass -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.name} => 'pig' NoMethodError: undefined method `name' for nil:NilClass -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.nil? ? 'no animal' : animal.name} => 'pig' 'no animal' 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.nil? ? 'no animal' : animal.name} => 'pig' 'no animal' 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.nil? ? 'no animal' : animal.name} => 'pig' 'no animal' 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.nil? ? 'no animal' : animal.name} => 'pig' 'no animal' 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.nil? ? 'no animal' : animal.name} => 'pig' 'no animal' 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.nil? ? 'no animal' : animal.name} => 'pig' 'no animal' 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal && animal.name } => 'pig' empty string 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal && animal.name } => 'pig' empty string 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal && animal.name } => 'pig' empty string 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal && animal.name } => 'pig' empty string 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal && animal.name } => 'pig' empty string 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal && animal.name } => 'pig' empty string 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.try(:name)} => 'pig' empty string 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.try(:name)} => 'pig' empty string 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.try(:name)} => 'pig' empty string 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal:0x007f94b290ae90 @name="pig">, nil, #<Animal:0x007f94b290ae18 @name="sheep">] animals.each {|animal| puts animal.try(:name)} => 'pig' empty string 'sheep' -
Jan 2015 @sandimetz puts animal.try(:name) puts animal.nil? ? ''
: animal.name puts animal == nil ? '' : animal.name puts animal.is_a?(NilClass) ? '' : animal.name -
Jan 2015 @sandimetz puts animal.try(:name) puts animal.nil? ? ''
: animal.name puts animal == nil ? '' : animal.name puts animal.is_a?(NilClass) ? '' : animal.name -
Jan 2015 @sandimetz puts animal.try(:name) puts animal.nil? ? ''
: animal.name puts animal == nil ? '' : animal.name puts animal.is_a?(NilClass) ? '' : animal.name -
Jan 2015 @sandimetz puts animal.try(:name) puts animal.nil? ? ''
: animal.name puts animal == nil ? '' : animal.name puts animal.is_a?(NilClass) ? '' : animal.name -
Jan 2015 @sandimetz puts animal.is_a?(NilClass) ? '' : animal.name
if animal.is_a?(NilClass) puts '' else puts animal.name end -
Jan 2015 @sandimetz puts animal.is_a?(NilClass) ? '' : animal.name
if animal.is_a?(NilClass) puts '' else puts animal.name end -
Jan 2015 @sandimetz if animal.is_a?(NilClass) puts ''
else puts animal.name end -
Jan 2015 @sandimetz if animal.is_a?(NilClass) puts ''
else puts animal.name end -
Jan 2015 @sandimetz if animal.is_a?(NilClass) puts ''
else puts animal.name end -
Jan 2015 @sandimetz if (some object whose type I know)
code to do some stuff else code to do some other stuff end if animal.is_a?(NilClass) puts 'no animal' else puts animal.name end -
Jan 2015 @sandimetz if (some object whose type I know)
code to do some stuff else code to do some other stuff end if animal.is_a?(NilClass) puts 'no animal' else puts animal.name end -
Jan 2015 @sandimetz if (some object whose type I know)
code to do some stuff else code to do some other stuff end if animal.is_a?(NilClass) puts 'no animal' else puts animal.name end -
Jan 2015 @sandimetz if (some object whose type I know)
I'll supply the behavior else code to do some other stuff end if animal.is_a?(NilClass) puts 'no animal' else puts animal.name end -
Jan 2015 @sandimetz if (some object whose type I know)
I'll supply the behavior else code to do some other stuff end if animal.is_a?(NilClass) puts 'no animal' else puts animal.name end -
Jan 2015 @sandimetz if (some object whose type I know)
I'll supply the behavior else code to do some other stuff end if animal.is_a?(NilClass) puts 'no animal' else puts animal.name end -
Jan 2015 @sandimetz if animal.is_a?(NilClass) puts 'no animal'
else puts animal.name end if (some object whose type I know) I'll supply the behavior else I'll send a message end -
Jan 2015 @sandimetz class Animal def self.find animal =
Animal.find(id) animal.nil? ? 'no animal' : animal.name -
Jan 2015 @sandimetz class Animal def self.find animal =
Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name -
Jan 2015 @sandimetz class Animal def self.find ind(id)
'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name Animal.find(id) ? 'no animal' : animal.name -
Jan 2015 @sandimetz class Animal def self.find ind(id)
'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name Animal.find(id) ? 'no animal' : animal.name find(id) animal' : animal.name d) imal' : animal.name -
Jan 2015 @sandimetz class Animal def self.find ind(id)
'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name Animal.find(id) ? 'no animal' : animal.name find(id) animal' : animal.name d) imal' : animal.name 'no animal' -
Jan 2015 @sandimetz class Animal def self.find ind(id)
'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name Animal.find(id) ? 'no animal' : animal.name find(id) animal' : animal.name d) imal' : animal.name 'no animal' 'no animal' -
Jan 2015 @sandimetz class Animal def self.find ind(id)
'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name Animal.find(id) ? 'no animal' : animal.name find(id) animal' : animal.name d) imal' : animal.name 'no animal' 'no animal' 'no animal' -
Jan 2015 @sandimetz class Animal def self.find ind(id)
'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name Animal.find(id) ? 'no animal' : animal.name find(id) animal' : animal.name d) imal' : animal.name 'no animal' 'no animal' 'no animal' 'no animal' -
Jan 2015 @sandimetz class Animal def self.find ind(id)
'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name animal = Animal.find(id) animal.nil? ? 'no animal' : animal.name Animal.find(id) ? 'no animal' : animal.name find(id) animal' : animal.name d) imal' : animal.name 'no animal' 'no animal' 'no animal' 'no animal' 'no animal' !!!! -
Jan 2015 @sandimetz if animal.is_a?(NilClass) puts 'no animal'
else puts animal.name end if (some object whose type I know) I'll supply the behavior else I'll send a message end -
Jan 2015 @sandimetz if animal.is_a?(NilClass) puts 'no animal'
else puts animal.name end -
Jan 2015 @sandimetz if animal.is_a?(NilClass) puts 'no animal'
else puts animal.name end -
Jan 2015 @sandimetz class Animal def name
... end end -
Jan 2015 @sandimetz class Animal def name
... end end class Nil # does not understand name end -
Jan 2015 @sandimetz class Animal def name
... end end class ??? def name 'no animal' end end -
Jan 2015 @sandimetz class Animal def name
... end end class MissingAnimal def name 'no animal' end end -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id)} => [#<Animal: @name="pig">, nil, #<Animal: @name="sheep">] animals.each {|animal| if animal.is_a?(NilClass) puts 'no animal' else puts animal.name end } -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id) || MissingAnimal.new} => [#<Animal: @name="pig">, nil, #<Animal: @name="sheep">] animals.each {|animal| if animal.is_a?(NilClass) puts 'no animal' else puts animal.name end } -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id) || MissingAnimal.new} => [#<Animal: @name="pig">, nil, #<Animal: @name="sheep">] animals.each {|animal| if animal.is_a?(NilClass) puts 'no animal' else puts animal.name end } -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id) || MissingAnimal.new} => [#<Animal: @name="pig">, nil, #<Animal: @name="sheep">] animals.each {|animal| if animal.is_a?(NilClass) puts 'no animal' else puts animal.name end } -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= 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 } -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= 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 } added dependency -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= 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 } -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= 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 } still have the condition -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= 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 -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= 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 } -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id) || MissingAnimal.new} => [#<Animal: @name="pig">, #<MissingAnimal:>, #<Animal: @name="sheep">] animals.each {|animal| puts animal.name } -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id) || MissingAnimal.new} => [#<Animal: @name="pig">, #<MissingAnimal:>, #<Animal: @name="sheep">] animals.each {|animal| puts animal.name } -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id) || MissingAnimal.new} => [#<Animal: @name="pig">, #<MissingAnimal:>, #<Animal: @name="sheep">] animals.each {|animal| puts animal.name} -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id) || MissingAnimal.new} => [#<Animal: @name="pig">, #<MissingAnimal:>, #<Animal: @name="sheep">] animals.each {|animal| puts animal.name} => 'pig' 'no animal' 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id) || MissingAnimal.new} => [#<Animal: @name="pig">, #<MissingAnimal:>, #<Animal: @name="sheep">] animals.each {|animal| puts animal.name} => 'pig' 'no animal' 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id) || MissingAnimal.new} => [#<Animal: @name="pig">, #<MissingAnimal:>, #<Animal: @name="sheep">] animals.each {|animal| puts animal.name} => 'pig' 'no animal' 'sheep' -
Jan 2015 @sandimetz class Animal def name
... end end class MissingAnimal def name 'no animal' end end -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id) || MissingAnimal.new} animals.each {|animal| puts animal.name} => 'pig' 'no animal' 'sheep' -
Jan 2015 @sandimetz ids = ['pig', '', 'sheep'] animals
= ids.map {|id| Animal.find(id) || MissingAnimal.new} animals.each {|animal| puts animal.name} => 'pig' 'no animal' 'sheep' dependency -
Jan 2015 @sandimetz class Animal def self.find animal =
Animal.find(id) || MissingAnimal.new animal.name -
Jan 2015 @sandimetz class Animal def self.find animal =
Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name -
Jan 2015 @sandimetz class Animal def self.find || MissingAnimal.new
animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name al.find(id) || MissingAnimal.new -
Jan 2015 @sandimetz class Animal def self.find || MissingAnimal.new
animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name al.find(id) || MissingAnimal.new ) || MissingAnimal.new issingAnimal.new -
Jan 2015 @sandimetz class Animal def self.find || MissingAnimal.new
animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name al.find(id) || MissingAnimal.new ) || MissingAnimal.new issingAnimal.new MissingAnimal -
Jan 2015 @sandimetz class Animal def self.find || MissingAnimal.new
animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name al.find(id) || MissingAnimal.new ) || MissingAnimal.new issingAnimal.new MissingAnimal MissingAnimal -
Jan 2015 @sandimetz class Animal def self.find || MissingAnimal.new
animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name al.find(id) || MissingAnimal.new ) || MissingAnimal.new issingAnimal.new MissingAnimal MissingAnimal MissingAnimal -
Jan 2015 @sandimetz class Animal def self.find || MissingAnimal.new
animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name al.find(id) || MissingAnimal.new ) || MissingAnimal.new issingAnimal.new MissingAnimal MissingAnimal MissingAnimal MissingAnimal -
Jan 2015 @sandimetz class Animal def self.find || MissingAnimal.new
animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name animal = Animal.find(id) || MissingAnimal.new animal.name al.find(id) || MissingAnimal.new ) || MissingAnimal.new issingAnimal.new MissingAnimal MissingAnimal MissingAnimal MissingAnimal MissingAnimal ??? -
Jan 2015 @sandimetz class Animal def self.find(id)
# nil or an Animal end # … end -
Jan 2015 @sandimetz class GuaranteedAnimal def self.find class Animal
def self.find class GuaranteedAnimal def self.find(id) Animal.find(id) || MissingAnimal.new end end -
Jan 2015 @sandimetz animals = ids.map {|id| GuaranteedAnimal.find(id)}
=> [#<Animal: @name="pig">, #<MissingAnimal:>, #<Animal: @name="sheep">] -
Jan 2015 @sandimetz animals = ids.map {|id| GuaranteedAnimal.find(id)}
=> [#<Animal: @name="pig">, #<MissingAnimal:>, #<Animal: @name="sheep">] -
Jan 2015 @sandimetz animals = ids.map {|id| GuaranteedAnimal.find(id)}
=> [#<Animal: @name="pig">, #<MissingAnimal:>, #<Animal: @name="sheep">] animals.each {|animal| puts animal.name } => 'pig' 'no animal' 'sheep' -
Jan 2015 @sandimetz animals = ids.map {|id| GuaranteedAnimal.find(id)}
=> [#<Animal: @name="pig">, #<MissingAnimal:>, #<Animal: @name="sheep">] animals.each {|animal| puts animal.name } => 'pig' 'no animal' 'sheep' -
Jan 2015 @sandimetz animals = ids.map {|id| GuaranteedAnimal.find(id)}
=> [#<Animal: @name="pig">, #<MissingAnimal:>, #<Animal: @name="sheep">] animals.each {|animal| puts animal.name } => 'pig' 'no animal' 'sheep' -
Jan 2015 @sandimetz 'This is the house that Jack
built.' 'This is the malt that lay in the house that Jack built.' 1 2 -
Jan 2015 @sandimetz '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.' 1 2 3 -
Jan 2015 @sandimetz '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.' '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 -
Jan 2015 @sandimetz [ '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' ] -
Jan 2015 @sandimetz 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 -
Jan 2015 @sandimetz 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 -
Jan 2015 @sandimetz def phrase(number)
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 -
Jan 2015 @sandimetz def phrase(number)
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'] -
Jan 2015 @sandimetz 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 -
Jan 2015 @sandimetz 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 -
Jan 2015 @sandimetz 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 -
Jan 2015 @sandimetz 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 -
Jan 2015 @sandimetz 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 -
Jan 2015 @sandimetz def recite
(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 -
Jan 2015 @sandimetz def recite
(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 -
Jan 2015 @sandimetz class House def recite
(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 -
Jan 2015 @sandimetz puts House.new.line(1) => 'This is the house
that Jack built.' -
Jan 2015 @sandimetz puts House.new.line(1) => 'This is the house
that Jack built.' puts House.new.line(2) => 'This is the malt that lay in the house that Jack built.' -
Jan 2015 @sandimetz puts House.new.line(1) => 'This is the house
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.' -
Jan 2015 @sandimetz puts House.new.line(1) => 'This is the house
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.' -
Jan 2015 @sandimetz puts House.new.line(1) => 'This is the house
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 -
Jan 2015 @sandimetz [ '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' ] -
Jan 2015 @sandimetz [ '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'] -
Jan 2015 @sandimetz [ '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'] -
Jan 2015 @sandimetz [ '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'] -
Jan 2015 @sandimetz [ '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'] -
Jan 2015 @sandimetz 'This is the rat that ate.'
'This is the maiden all forlorn that milked the rat that ate.' 1 2 -
Jan 2015 @sandimetz 'This is 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 -
Jan 2015 @sandimetz 'This is 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.' '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 -
Jan 2015 @sandimetz 'This is 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.' '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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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? -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz randomhouse = RandomHouse.new puts randomhouse.line(1) => 'This
is the rat that ate.' -
Jan 2015 @sandimetz randomhouse = RandomHouse.new puts randomhouse.line(1) => 'This
is the rat that ate.' puts randomhouse.line(2) => 'This is the maiden all forlorn that milked the rat that ate.' -
Jan 2015 @sandimetz randomhouse = RandomHouse.new puts randomhouse.line(1) => 'This
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.' -
Jan 2015 @sandimetz randomhouse = RandomHouse.new puts randomhouse.line(1) => 'This
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.' -
Jan 2015 @sandimetz randomhouse = RandomHouse.new puts randomhouse.line(1) => 'This
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.' -
Jan 2015 @sandimetz randomhouse = RandomHouse.new puts randomhouse.line(1) => 'This
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.' -
Jan 2015 @sandimetz randomhouse = RandomHouse.new puts randomhouse.line(1) => 'This
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.' -
Jan 2015 @sandimetz This is the house that Jack built
the house that Jack built. -
Jan 2015 @sandimetz This is the house that Jack built
the house that Jack built. -
Jan 2015 @sandimetz This is the house that Jack built
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. -
Jan 2015 @sandimetz This is the house that Jack built
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. -
Jan 2015 @sandimetz This is the house that Jack built
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. -
Jan 2015 @sandimetz class House def recite
(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 -
Jan 2015 @sandimetz class House def recite
(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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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? -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz ['the rat that ate', 'the malt
that lay in', 'the house that Jack built'] -
Jan 2015 @sandimetz ['the rat that ate', 'the malt
that lay in', 'the house that Jack built'] ['the rat that ate', 'the malt that lay in', 'the house that Jack built'] -
Jan 2015 @sandimetz ['the rat that ate', 'the malt
that lay in', 'the house that Jack built'] ['the rat that ate', 'the malt that lay in', 'the house that Jack built'] super.zip(super) -
Jan 2015 @sandimetz ['the rat that ate', 'the malt
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) -
Jan 2015 @sandimetz ['the rat that ate', 'the malt
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) -
Jan 2015 @sandimetz ['the rat that ate', 'the malt
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'] -
Jan 2015 @sandimetz class House def recite
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 -
Jan 2015 @sandimetz puts EchoHouse.new.line(1) 'This is the house that
Jack built the house that Jack built.' -
Jan 2015 @sandimetz puts EchoHouse.new.line(1) 'This is the house that
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.' -
Jan 2015 @sandimetz puts EchoHouse.new.line(1) 'This is the house that
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.' -
Jan 2015 @sandimetz puts EchoHouse.new.line(1) 'This is the house that
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.' -
Jan 2015 @sandimetz class House def recite
def line(number) def phrase(number) def parts(number) def data end -
Jan 2015 @sandimetz class RandomHouse < House def
data @data ||= super.shuffle end end class House def recite def line(number) def phrase(number) def parts(number) def data end -
Jan 2015 @sandimetz class RandomHouse < House def
data @data ||= super.shuffle end end class House def recite def line(number) def phrase(number) def parts(number) def data end -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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? -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end -
Jan 2015 @sandimetz class RandomHouse < House def
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end -
Jan 2015 @sandimetz class RandomHouse < House def
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end Duplicate some of the code? -
Jan 2015 @sandimetz class RandomHouse < House def
data @data ||= super.shuffle end end class EchoHouse < House def parts(number) super.zip(super).flatten end end RandomEchoHouse < RandomHouse < EchoHouse data parts -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House class EchoHouse
< House Duplicate all of the code! -
Jan 2015 @sandimetz class RandomHouse < House class EchoHouse
< House class RandomEchoHouse < House -
Jan 2015 @sandimetz class RandomHouse < House class EchoHouse
< House class RandomEchoHouse < House -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class RandomHouse < House def
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 -
Jan 2015 @sandimetz class House 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 end class RandomHouse < House def data @data ||= super.shuffle end end -
Jan 2015 @sandimetz class House 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 end class RandomHouse < House def data @data ||= super.shuffle end end What does RandomHouse change? -
Jan 2015 @sandimetz class House 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 end class RandomHouse < House def data @data ||= super.shuffle end end -
Jan 2015 @sandimetz class House 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 end class RandomHouse < House def data @data ||= super.shuffle end end Make this -
Jan 2015 @sandimetz class House 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 end class RandomHouse < House def data @data ||= super.shuffle end end Make this More like this -
Jan 2015 @sandimetz class House 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 end -
Jan 2015 @sandimetz class House 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 -
Jan 2015 @sandimetz class House DATA =
[ 'the horse and the hound and the horn that belonged to', # ... 'the house that Jack built'] end -
Jan 2015 @sandimetz class House DATA =
[ 'the horse and the hound and the horn that belonged to', # ... 'the house that Jack built'] def data DATA end end -
Jan 2015 @sandimetz class House DATA =
[ '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 -
Jan 2015 @sandimetz class House DATA =
[ '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 -
Jan 2015 @sandimetz class House DATA =
[ '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 -
Jan 2015 @sandimetz class House DATA =
[ '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? -
Jan 2015 @sandimetz class data order House DATA RandomHouse DATA
shuffle This is not nothing -
Jan 2015 @sandimetz class data order House DATA RandomHouse DATA
shuffle It's an order algorithm -
Jan 2015 @sandimetz class data order House DATA RandomHouse DATA
shuffle It's an order algorithm just as valid as this one -
Jan 2015 @sandimetz class RandomOrder def order(data)
data.shuffle end end -
Jan 2015 @sandimetz class DefaultOrder end class RandomOrder
def order(data) data.shuffle end end -
Jan 2015 @sandimetz class DefaultOrder def order(data)
end end class RandomOrder def order(data) data.shuffle end end -
Jan 2015 @sandimetz class DefaultOrder def order(data)
data end end class RandomOrder def order(data) data.shuffle end end -
Jan 2015 @sandimetz class House DATA =
[ '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 -
Jan 2015 @sandimetz class House DATA =
[ 'the horse and the hound and the horn that belonged to', # ... 'the house that Jack built'] def data DATA end end -
Jan 2015 @sandimetz class House DATA =
[ 'the horse and the hound and the horn that belonged to', # ... 'the house that Jack built'] def data DATA end # ... end -
Jan 2015 @sandimetz class House DATA =
[ 'the horse and the hound and the horn that belonged to', # ... 'the house that Jack built'] attr_reader :data def data DATA end # ... end -
Jan 2015 @sandimetz class House DATA =
[ '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 -
Jan 2015 @sandimetz class House DATA =
[ '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 -
Jan 2015 @sandimetz class House DATA =
[ '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 -
Jan 2015 @sandimetz class House attr_reader :data
def initialize @data = DATA end # ... end -
Jan 2015 @sandimetz class House attr_reader :data
def initialize @data = DATA end # ... end -
Jan 2015 @sandimetz class House attr_reader :data
def initialize @data = DATA end # ... end Remove 'ordering' responsibility from House -
Jan 2015 @sandimetz class House attr_reader :data
def initialize @data = DATA end # ... end class DefaultOrder def order(data) data end end -
Jan 2015 @sandimetz class House attr_reader :data
def initialize(orderer: DefaultOrder.new) @data = DATA end # ... end class DefaultOrder def order(data) data end end -
Jan 2015 @sandimetz class House attr_reader :data
def initialize(orderer: DefaultOrder.new) @data = DATA end # ... end class DefaultOrder def order(data) data end end -
Jan 2015 @sandimetz class House attr_reader :data
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end -
Jan 2015 @sandimetz class House attr_reader :data
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultOrder def order(data) data end end puts House.new.line(12) -
Jan 2015 @sandimetz class House attr_reader :data
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.' -
Jan 2015 @sandimetz class House attr_reader :data
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.' -
Jan 2015 @sandimetz class House attr_reader :data
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 -
Jan 2015 @sandimetz class House attr_reader :data
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 -
Jan 2015 @sandimetz class House attr_reader :data
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 -
Jan 2015 @sandimetz class House attr_reader :data
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.' -
Jan 2015 @sandimetz class House attr_reader :data
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 -
Jan 2015 @sandimetz class House attr_reader :data
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 -
Jan 2015 @sandimetz class House attr_reader :data
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 -
Jan 2015 @sandimetz class House attr_reader :data
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 -
Jan 2015 @sandimetz class House attr_reader :data
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 -
Jan 2015 @sandimetz class House attr_reader :data
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 -
Jan 2015 @sandimetz class House attr_reader :data
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 -
Jan 2015 @sandimetz class House attr_reader :data
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 -
Jan 2015 @sandimetz class House attr_reader :data
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 -
Jan 2015 @sandimetz class EchoFormatter def format(parts)
parts.zip(parts).flatten end end -
Jan 2015 @sandimetz class DefaultFormatter def format(parts)
parts end end class EchoFormatter def format(parts) parts.zip(parts).flatten end end -
Jan 2015 @sandimetz class House attr_reader :data
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end -
Jan 2015 @sandimetz class House attr_reader :data
def initialize(orderer: DefaultOrder.new) @data = orderer.order(DATA) end # ... end class DefaultFormatter def format(parts) parts end end -
Jan 2015 @sandimetz class House attr_reader :data
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @data = orderer.order(DATA) end # ... end class DefaultFormatter def format(parts) parts end end -
Jan 2015 @sandimetz class House attr_reader :data
def initialize(orderer: DefaultOrder.new, formatter: DefaultFormatter.new) @data = orderer.order(DATA) end # ... end class DefaultFormatter def format(parts) parts end end -
Jan 2015 @sandimetz class House attr_reader :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 -
Jan 2015 @sandimetz class House attr_reader :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 -
Jan 2015 @sandimetz class House attr_reader
: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 -
Jan 2015 @sandimetz class House attr_reader :formatter, :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 -
Jan 2015 @sandimetz class House attr_reader :formatter, :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 -
Jan 2015 @sandimetz class House attr_reader :formatter, :data
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 -
Jan 2015 @sandimetz class House attr_reader :formatter, :data
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 -
Jan 2015 @sandimetz class House attr_reader :formatter, :data
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 -
Jan 2015 @sandimetz class House attr_reader :formatter, :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 -
Jan 2015 @sandimetz class House attr_reader :formatter, :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 -
Jan 2015 @sandimetz 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.line(12) class DefaultFormatter def format(parts) parts end end -
Jan 2015 @sandimetz 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.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 -
Jan 2015 @sandimetz class DefaultFormatter def format(parts)
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 -
Jan 2015 @sandimetz class DefaultFormatter def format(parts)
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 -
Jan 2015 @sandimetz class DefaultFormatter def format(parts)
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 -
Jan 2015 @sandimetz class DefaultOrder def order(data)
data end end class RandomOrder def order(data) data.shuffle end end Orderer Role -
Jan 2015 @sandimetz class DefaultFormatter def format(parts)
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 -
Jan 2015 @sandimetz puts House.new.recite puts House.new(orderer: RandomOrder.new).recite puts House.new(formatter:
EchoFormatter.new).recite puts House.new(orderer: RandomOrder.new, formatter: EchoFormatter.new).recite -
Jan 2015 @sandimetz puts House.new.recite puts House.new(orderer: RandomOrder.new).recite
puts House.new(formatter: EchoFormatter.new).recite puts House.new(orderer: RandomOrder.new, formatter: EchoFormatter.new).recite More code? -
Jan 2015 @sandimetz puts House.new.recite puts House.new(orderer: RandomOrder.new).recite
puts House.new(formatter: EchoFormatter.new).recite puts House.new(orderer: RandomOrder.new, formatter: EchoFormatter.new).recite -
Jan 2015 @sandimetz puts House.new.recite puts House.new(orderer: RandomOrder.new).recite
puts House.new(formatter: EchoFormatter.new).recite puts House.new(orderer: RandomOrder.new, formatter: EchoFormatter.new).recite Less code! -
Jan 2015 @sandimetz puts House.new.recite puts House.new(orderer: RandomOrder.new).recite
puts House.new(formatter: EchoFormatter.new).recite puts House.new(orderer: RandomOrder.new, formatter: EchoFormatter.new).recite Less code! No duplication -
Jan 2015 @sandimetz puts House.new.recite puts House.new(orderer: RandomOrder.new).recite
puts House.new(formatter: EchoFormatter.new).recite puts House.new(orderer: RandomOrder.new, formatter: EchoFormatter.new).recite Less code! Pluggable behavior -
Jan 2015 @sandimetz puts House.new.recite puts House.new(orderer: RandomOrder.new).recite
puts House.new(formatter: EchoFormatter.new).recite puts House.new(orderer: RandomOrder.new, formatter: EchoFormatter.new).recite -
Jan 2015 @sandimetz puts House.new.recite puts House.new(orderer: RandomOrder.new).recite
puts House.new(formatter: EchoFormatter.new).recite puts House.new(orderer: RandomOrder.new, formatter: EchoFormatter.new).recite Composition -
Jan 2015 @sandimetz puts House.new.recite puts House.new(orderer: RandomOrder.new).recite
puts House.new(formatter: EchoFormatter.new).recite puts House.new(orderer: RandomOrder.new, formatter: EchoFormatter.new).recite Composition + Dependency Injection -
Jan 2015 @sandimetz puts House.new.recite puts House.new(orderer: RandomOrder.new).recite
puts House.new(formatter: EchoFormatter.new).recite puts House.new(orderer: RandomOrder.new, formatter: EchoFormatter.new).recite Object-Oriented Design -
Jan 2015 @sandimetz Isolate the thing that varies Name the
concept Define the role -
Jan 2015 @sandimetz Isolate the thing that varies Name the
concept Define the role Inject the players