Skip to content

Instantly share code, notes, and snippets.

@DavidMikeSimon
Last active December 13, 2019 00:23
Show Gist options
  • Select an option

  • Save DavidMikeSimon/1b3ce6e411f1c65aef4f67edf56fb3e7 to your computer and use it in GitHub Desktop.

Select an option

Save DavidMikeSimon/1b3ce6e411f1c65aef4f67edf56fb3e7 to your computer and use it in GitHub Desktop.
Ruby DRY vs DAMP
def shout(msg)
puts "#{msg}!!!"
end
def shout_twice(msg)
puts "#{msg}!!! #{msg}!!!"
end
def louden(msg)
"#{msg}!!!"
end
def shout(msg)
puts louden(msg)
end
def shout_twice(msg)
puts "#{louden(msg)} #{louden(msg)}"
end
describe Shouter do
before { @shouter = Shouter.new(loudness: 3) }
# a few hundred lines later ...
it "shouts loudly" do
expect { @shouter.shout("Heya") }
.to_output("Heya!!!")
end
it "yells loudly" do
expect { @shouter.yell(true) }
.to_output("Yep!!!")
end
end
describe Shouter do
# a few hundred lines later ...
it "shouts loudly" do
sh = Shouter.new(3)
expect { sh.shout("Heya") }
.to_output("Heya!!!")
end
it "yells loudly" do
sh = Shouter.new(3)
expect { sh.yell(true) }
.to_output("Yep!!!")
end
end
describe Stuff do
before { @sh = Shouter.new(loudness: 3) }
# ...
it "yells softly" do
expect { @shouter.shout("Hi") }
.to_output("Hi")
end
end
describe Stuff do
before { @sh = Shouter.new(3) }
# ...
context "when whisper mode is true" do
before { Settings.whisper = true }
# ...
it "yells softly" do
expect { @shouter.shout("Hi") }
.to_output("Hi")
end
end
describe Stuff do
before { @sh = Shouter.new(3) }
# ...
context "when whisper mode is true" do
before { Settings.whisper = true }
# ...
context "when loudness is 0" do
before { @sh = Shouter.new(0) }
# ...
it "yells softly" do
expect { @shouter.shout("Hi") }
.to_output("Hi")
end
end
describe Shouter do
it "can shout loudly" do
Shouter::Settings.whisper = false
sh = Shouter.new(3, :foo, :bar)
expect { sh.shout("Heya") }
.to_output("Heya!!!")
end
it "can shout softly" do
Shouter::Settings.whisper = true
sh = Shouter.new(0, :foo, :bar)
expect { sh.shout("Heya") }
.to_output("Heya")
end
end
describe Shouter do
it "can shout loudly" do
sh = make_shouter(volume: 3)
expect { sh.shout("Heya") }
.to_output("Heya!!!")
end
it "can shout softly" do
sh = make_shouter(volume: 0)
expect { sh.shout("Heya") }
.to_output("Heya")
end
def make_shouter(volume:)
Shouter::Settings.whisper =
(volume == 0)
return Shouter.new(vol, :foo, :bar)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment