Skip to content

Instantly share code, notes, and snippets.

@seashootz
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save seashootz/e987965f7b2419cdd6d2 to your computer and use it in GitHub Desktop.

Select an option

Save seashootz/e987965f7b2419cdd6d2 to your computer and use it in GitHub Desktop.
Fizzbuzz

Fizzbuzz

There are many ways to do this kata, including defining a refinement on the integer class as I did here. There are also many ways to print the fizzbuzz data including converting an integer to its representative in fizzbuzz or creating an array of numbers up to that integer with their fizzbuzz contents.

Printing to STDOUT

To print each element of an array to STDOUT it can be as simple as requiring a puts in front of the array. Or if the program is intended to be a ruby script, calling #each and printing each element of that array. I do not think that creating an array that prints each line should be part of this refinement, but instead could be a refinement on the array class, or a special class by itself with a method like:

def print_each(array)
  array.each do |element|
    p element
  end
end

To create an executable script so that this: ruby fizz_buzz.rb 3 prints:

1
2
fizz

Please see the file print_fizzbuzz, because we have tested the refinement, we can be sure that the correct numbers are printed to STDOUT

module Fizzbuzz
refine Integer do
def to_fizzbuzz
case
when self.fizzbuzz? then 'fizzbuzz'
when self.buzz? then 'buzz'
when self.fizz? then 'fizz'
else self
end
end
def fizzbuzz_array
return [] if self < 1
# unfortunately symbol_to_proc does not work yet for refinements
# i.e. (1..self).map(&:to_fizzbuzz)
(1..self).map{ |int| int.to_fizzbuzz }
end
def fizzbuzz?
self % 15 == 0
end
def buzz?
self % 5 == 0
end
def fizz?
self % 3 == 0
end
end
end
# this spec requires RSpec installed
require_relative 'fizzbuzz'
describe 'Fizzbuzz' do
using Fizzbuzz
context '#to_fizzbuzz' do
specify { expect(1.to_fizzbuzz).to eq 1 }
specify { expect(2.to_fizzbuzz).to eq 2 }
specify { expect(3.to_fizzbuzz).to eq 'fizz' }
specify { expect(5.to_fizzbuzz).to eq 'buzz' }
specify { expect(6.to_fizzbuzz).to eq 'fizz' }
specify { expect(7.to_fizzbuzz).to eq 7 }
specify { expect(15.to_fizzbuzz).to eq 'fizzbuzz' }
end
context '#to_fizzbuzz array' do
specify { expect(-1.fizzbuzz_array).to eq [] }
specify { expect(0.fizzbuzz_array).to eq [] }
specify { expect(1.fizzbuzz_array).to eq [1] }
specify { expect(2.fizzbuzz_array).to eq [1, 2] }
specify { expect(3.fizzbuzz_array).to eq [1, 2, 'fizz'] }
specify { expect(5.fizzbuzz_array).to eq [1, 2, 'fizz', 4, 'buzz'] }
end
end
require_relative 'fizzbuzz'
using Fizzbuzz
# ARGV is an array of string arguments passed in after ``ruby <filename>``
# As such #first and #to_i are necessary to convert it to an integer/fixnum
# to use the refinement defined in fizzbuzz.rb.
# with this, you can print to STDOUT with
# ruby print_fizzbuzz.rb 15
puts ARGV.first.to_i.fizzbuzz_array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment