Created
April 5, 2013 23:46
-
-
Save vinyll/5323529 to your computer and use it in GitHub Desktop.
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 | |
def initialize | |
@fizz = 3 | |
@buzz = 5 | |
end | |
def convert(num) | |
return "FizzBuzz" if isDivisibleBy(@fizz, num) && isDivisibleBy(@buzz, num) | |
return "Fizz" if isDivisibleBy(@fizz, num) | |
return "Buzz" if isDivisibleBy(@buzz, num) | |
num | |
end | |
def loop() | |
for i in 1..100 | |
puts convert(i) | |
end | |
end | |
def isDivisibleBy(divisor, num) | |
num % divisor == 0 | |
end | |
end | |
fb = FizzBuzz.new() | |
fb.loop() | |
## TESTS | |
require "./fizzbuzz" | |
require "test/unit" | |
class TestFizzBuzz < Test::Unit::TestCase | |
def test_should_return_1_when_1 | |
assert_equal(1, FizzBuzz.new().convert(1)) | |
end | |
def test_should_return_2_when_2 | |
assert_equal(2, FizzBuzz.new().convert(2)) | |
end | |
def test_should_return_Fizz_when_3 | |
assert_equal("Fizz", FizzBuzz.new().convert(3)) | |
end | |
def test_should_return_Buzz_when_5 | |
assert_equal("Buzz", FizzBuzz.new().convert(5)) | |
end | |
def test_should_return_Fizz_when_6 | |
assert_equal("Fizz", FizzBuzz.new().convert(6)) | |
end | |
def test_should_return_Fizz_when_9 | |
assert_equal("Fizz", FizzBuzz.new().convert(9)) | |
end | |
def test_should_return_Buzz_when_10 | |
assert_equal("Buzz", FizzBuzz.new().convert(10)) | |
end | |
def test_should_return_FizzBuzz_when_15 | |
assert_equal("FizzBuzz", FizzBuzz.new().convert(15)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment