Skip to content

Instantly share code, notes, and snippets.

@tribals
Last active October 3, 2020 10:02
Show Gist options
  • Save tribals/b5f98b2799890ff343baf589901610d4 to your computer and use it in GitHub Desktop.
Save tribals/b5f98b2799890ff343baf589901610d4 to your computer and use it in GitHub Desktop.
OO-style FuzzBuzz solution in Ruby
class FuzzBuzz
attr_reader :n, :fuzzbuzzable
def initialize(n, fuzzbuzzable)
@n = n
@fuzzbuzzable = fuzzbuzzable
end
def do_it
n.times do |i|
fuzzbuzzable.fuzz(i) if i % 3 == 0
fuzzbuzzable.buzz(i) if i % 5 == 0
end
end
end
class PrintToStdoutFuzzBuzzable
def fuzz(i)
puts "#{i}: fuzz"
end
def buzz(i)
puts "#{i}: buzz"
end
end
if __FILE__ == $0
fuzzbuzzable = PrintToStdoutFuzzBuzzable.new
fuzzbuzz = FuzzBuzz.new(100, fuzzbuzzable)
fuzzbuzz.do_it
end
require_relative 'fuzzbuzz'
shared_examples 'fuzzbuzz duck' do
let(:duck) { described_class.new }
it 'implements fuzzbuzz interface' do
expect(duck).to respond_to(:fuzz, :buzz)
end
end
describe PrintToStdoutFuzzBuzzable do
include_examples 'fuzzbuzz duck'
end
describe FuzzBuzz do
describe '#do_it' do
it 'performs main algorithm' do
fuzzbuzzable = instance_double('PrintToStdoutFuzzBuzzable')
fuzzbuzz = FuzzBuzz.new(16, fuzzbuzzable)
expect(fuzzbuzzable).to receive(:fuzz).exactly(5).times
expect(fuzzbuzzable).to receive(:buzz).exactly(3).times
expect(fuzzbuzzable).to receive(:fuzz).with(15).ordered
expect(fuzzbuzzable).to receive(:buzz).with(15).ordered
fuzzbuzz.do_it
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment