Skip to content

Instantly share code, notes, and snippets.

@shen-sat
Created September 10, 2024 18:04
Show Gist options
  • Save shen-sat/d1dbf34f0dfc5d42bbb9e33803d8ff89 to your computer and use it in GitHub Desktop.
Save shen-sat/d1dbf34f0dfc5d42bbb9e33803d8ff89 to your computer and use it in GitHub Desktop.
# fizzbuzz.rb
def fizzbuzz(number)
return "FizzBuzz" if number % 3 == 0 && number % 5 == 0
return "Fizz" if number % 3 == 0
return "Buzz" if number % 5 == 0
number
end
# fizzbuzz_spec.rb
require './fizzbuzz'
RSpec.describe 'FizzBuzz' do
it "returns 'Fizz' when divisible by 3" do
expect(fizzbuzz(3)).to eq('Fizz')
expect(fizzbuzz(6)).to eq('Fizz')
end
it "returns 'Buzz' when divisible by 5" do
expect(fizzbuzz(5)).to eq('Buzz')
expect(fizzbuzz(10)).to eq('Buzz')
end
it "returns 'FizzBuzz' when divisible by both 3 and 5" do
expect(fizzbuzz(15)).to eq('FizzBuzz')
expect(fizzbuzz(30)).to eq('FizzBuzz')
end
it "returns the number when not divisible by 3 or 5" do
expect(fizzbuzz(1)).to eq(1)
expect(fizzbuzz(4)).to eq(4)
expect(fizzbuzz(7)).to eq(7)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment