Last active
December 17, 2015 04:09
-
-
Save koriroys/5548457 to your computer and use it in GitHub Desktop.
Death of Ifs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Original method | |
#def process(input) | |
# if input == 'q' | |
# puts 'Goodbye' | |
# elsif input == 'tweet' | |
# puts 'tweeting' | |
# elsif input == 'dm' | |
# puts 'direct messaging' | |
# elsif input == 'help' | |
# puts 'helping' | |
# end | |
#end | |
RESPONSES = { | |
'tweet' => 'tweeting', | |
'dm' => 'direct messaging', | |
'help' => 'helping', | |
'q' => 'Goodbye' | |
} | |
def process(input) | |
puts RESPONSES.fetch(input) { return nil } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe 'process' do | |
it 'prints "Goodbye" when called with input of "q"' do | |
STDOUT.should_receive(:puts).with('Goodbye') | |
process('q') | |
end | |
it 'prints "tweeting" when called with input of "tweet"' do | |
STDOUT.should_receive(:puts).with('tweeting') | |
process('tweet') | |
end | |
it 'prints "direct messaging" when called with input of "dm"' do | |
STDOUT.should_receive(:puts).with('direct messaging') | |
process('dm') | |
end | |
it 'prints "helping" when called with input of "help"' do | |
STDOUT.should_receive(:puts).with('helping') | |
process('help') | |
end | |
it 'returns nil when called with any other input ' do | |
STDOUT.should_not_receive(:puts) | |
expect(process('bad input')).to eq(nil) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment