Skip to content

Instantly share code, notes, and snippets.

@just3ws
Created September 6, 2025 18:25
Show Gist options
  • Save just3ws/275748341ac041f5527919bca43c73ed to your computer and use it in GitHub Desktop.
Save just3ws/275748341ac041f5527919bca43c73ed to your computer and use it in GitHub Desktop.
Doodled a fizzbuzz just because.
# frozen_string_literal: true
require 'rspec/autorun'
# The rules of FizzBuzz are to count up, replacing numbers divisible by 3 with "Fizz,"
# numbers divisible by 5 with "Buzz," and
# numbers divisible by both 3 and 5 with "FizzBuzz";
# otherwise, say the number itself.
class FizzBuzz # :nodoc:
def self.play(num)
by_three = (num % 3).zero?
by_five = (num % 5).zero?
return 'FizzBuzz' if by_three && by_five
return 'Fizz' if by_three
return 'Buzz' if by_five
num.to_s
end
end
RSpec.describe 'FizzBuzz Game rules by example' do
subject(:game) { FizzBuzz }
it 'one is not divisible by 3' do
expect(game.play(1)).to eq 1.to_s
end
it 'two is not divisible by 3' do
expect(game.play(2)).to eq 2.to_s
end
it 'three is divisible by 3' do
expect(game.play(3)).to eq 'Fizz'
end
it 'four is not divisible by 3' do
expect(game.play(4)).to eq 4.to_s
end
context 'five is not divisible by 3' do
it 'five is divisible by 5 though' do
expect(game.play(5)).to eq 'Buzz'
end
end
it 'six is divisible by 3' do
expect(game.play(6)).to eq 'Fizz'
end
it 'seven is not divisible by 3' do
expect(game.play(7)).to eq 7.to_s
end
context 'we have to discuss 15' do
it '15 is divisible by 5 though too' do
expect(game.play(15)).to eq 'FizzBuzz'
end
end
it 'nor is 41 divisible by 3' do
expect(game.play(41)).to eq 41.to_s
end
it '42 is divisible by 3' do
expect(game.play(42)).to eq 'Fizz'
end
it '43 and 44 are not divisible by 3' do
expect(game.play(43)).to eq 43.to_s
expect(game.play(44)).to eq 44.to_s
end
context 'we have to discuss 45' do
it '45 is divisible by 5 though too' do
expect(game.play(45)).to eq 'FizzBuzz'
end
end
context 'we have to discuss 55' do
it '55 is not divisible by 3 though too' do
expect(game.play(55)).to eq 'Buzz'
end
end
end
__END__
FizzBuzz Game rules by example
one is not divisible by 3
two is not divisible by 3
three is divisible by 3
four is not divisible by 3
six is divisible by 3
seven is not divisible by 3
nor is 41 divisible by 3
42 is divisible by 3
43 and 44 are not divisible by 3
five is not divisible by 3
five is divisible by 5 though
we have to discuss 15
15 is divisible by 5 though too
we have to discuss 45
45 is divisible by 5 though too
we have to discuss 55
55 is not divisible by 3 though too
Finished in 0.0025 seconds (files took 0.0707 seconds to load)
13 examples, 0 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment