Created
January 22, 2014 03:52
-
-
Save srt32/8553226 to your computer and use it in GitHub Desktop.
Fizzbuzz with no ifs
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
class Fizzbuzz < Struct.new(:max) | |
MUTATIONS = { | |
'fizzbuzz' => ->(i) {i % 3 == 0 && i % 5 == 0}, | |
'fizz' => ->(i) {i % 3 == 0}, | |
'buzz' => ->(i) {i % 5 == 0} | |
} | |
def fizzle | |
1.upto(max).map do |i| | |
mutations.find(-> {[i]}) do |k, v| | |
v.call(i) | |
end[0] | |
end | |
end | |
private | |
def mutations | |
MUTATIONS | |
end | |
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 'minitest' | |
require 'minitest/autorun' | |
require 'minitest/pride' | |
require_relative 'fizzbuzz' | |
class FizzbuzzTest < Minitest::Test | |
def test_it_accepts_a_max_point | |
fizzbuzz = Fizzbuzz.new(1) | |
assert_equal fizzbuzz.max, 1 | |
end | |
def test_it_handles_upto_2 | |
fizzbuzz = Fizzbuzz.new(2) | |
assert_equal fizzbuzz.fizzle, [1,2] | |
end | |
def test_it_returns_fizz_for_3 | |
fizzbuzz = Fizzbuzz.new(3) | |
assert_equal fizzbuzz.fizzle, [1, 2, 'fizz'] | |
end | |
def test_it_returns_fizz_for_5 | |
fizzbuzz = Fizzbuzz.new(5) | |
assert_equal fizzbuzz.fizzle, [1, 2, 'fizz', 4, 'buzz'] | |
end | |
def test_it_returns_fizz_for_10 | |
fizzbuzz = Fizzbuzz.new(10) | |
assert_equal fizzbuzz.fizzle, [1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz'] | |
end | |
def test_it_returns_fizz_for_15 | |
fizzbuzz = Fizzbuzz.new(15) | |
assert_equal fizzbuzz.fizzle, [1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz'] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LEGIT!