Created
August 30, 2013 00:58
-
-
Save broguinn/6385231 to your computer and use it in GitHub Desktop.
raindrops
This file contains 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
def raindrops(number) | |
raise TypeError.new "the number must be an integer" if !(number.is_a?(Integer)) | |
phrase = "" | |
phrase << "Pling" if number % 3 == 0 | |
phrase << "Plang" if number % 5 == 0 | |
phrase << "Plong" if number % 7 == 0 | |
(phrase == "") ? number.to_s : phrase | |
end |
This file contains 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
require 'rspec' | |
require 'raindrops' | |
describe 'raindrops' do | |
it 'raises exceptions for non-integers' do | |
expect { raindrops(3.5) }.to raise_exception | |
expect { raindrops("foo") }.to raise_exception | |
expect { raindrops([14]) }.to raise_exception | |
end | |
it 'returns \'pling\' for numbers that have 3 as a prime factor' do | |
raindrops(6).should eq 'Pling' | |
end | |
it 'returns \'plang\' for numbers that have 5 as a prime factor' do | |
raindrops(25).should eq 'Plang' | |
end | |
it 'returns \'plong\' for numbers that have 7 as a prime factor' do | |
raindrops(14).should eq 'Plong' | |
end | |
it 'returns multiple phrases for multiple factors' do | |
raindrops(1755).should eq 'PlingPlang' | |
raindrops(105).should eq 'PlingPlangPlong' | |
end | |
it 'returns the original number in a string if 3, 5, or 7 are not prime factors' do | |
raindrops(34).should eq '34' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment