Last active
December 20, 2015 08:19
-
-
Save alvarezloaiciga/6099742 to your computer and use it in GitHub Desktop.
Fizzbuzz implementation
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
def fizzbuzz | |
(1..100).fizzbuzzfy | |
end | |
######################################### | |
############# Monkey Patches ############ | |
######################################### | |
class Range | |
def fizzbuzzfy | |
self.map(&:to_fizzbuzz).join(" ") | |
end | |
end | |
class Fixnum | |
def to_fizzbuzz | |
fizzbuzzable? ? fizzbuzz_word : self | |
end | |
private | |
def fizzbuzzable? | |
multiple_of_3? or multiple_of_5? | |
end | |
def fizzbuzz_word | |
return "FizzBuzz" if multiple_of_3? and multiple_of_5? | |
return "Fizz" if multiple_of_3? | |
return "Buzz" if multiple_of_5? | |
end | |
#This method is an alternative implementation of the above method | |
#it improves performance but looses readability. | |
#def fizzbuzz_word | |
# word = "" | |
# word << "Fizz" if multiple_of_3? | |
# word << "Buzz" if multiple_of_5? | |
# word | |
#end | |
def multiple_of_3? | |
self % 3 == 0 | |
end | |
def multiple_of_5? | |
self % 5 == 0 | |
end | |
end | |
################################# | |
########### SPECS ############### | |
################################# | |
describe "fizzbuzz" do | |
it "prints the fizzbuzz of the numbers from 1 to 100 inclusive" do | |
expect(fizzbuzz).to eq( | |
"1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz " + | |
"Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz " + | |
"41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz " + | |
"61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz " + | |
"Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz") | |
end | |
end | |
describe Range do | |
describe "fizzbuzzfy" do | |
it "creates a string with numbers to fizzbuzz" do | |
expect((1..15).fizzbuzzfy).to eq "1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz" | |
end | |
end | |
end | |
describe Fixnum do | |
describe "to_fizzbuzz" do | |
context "multiple of 3" do | |
it "returns 'Fizz' for 3" do | |
expect(3.to_fizzbuzz).to eq "Fizz" | |
end | |
it "returns 'Fizz' for 6" do | |
expect(6.to_fizzbuzz).to eq "Fizz" | |
end | |
end | |
context "multiple of 5" do | |
it "returns 'Buzz' for 5" do | |
expect(5.to_fizzbuzz).to eq "Buzz" | |
end | |
it "returns 'Buzz' for 10" do | |
expect(10.to_fizzbuzz).to eq "Buzz" | |
end | |
end | |
context "multiple of 3 and 5" do | |
it "returns 'FizzBuzz' for 15" do | |
expect(15.to_fizzbuzz).to eq "FizzBuzz" | |
end | |
it "returns 'FizzBuzz' for 30" do | |
expect(30.to_fizzbuzz).to eq "FizzBuzz" | |
end | |
end | |
context "not multiple of 3 or 5" do | |
it "returns 2 for 2" do | |
expect(2.to_fizzbuzz).to eq 2 | |
end | |
it "return 4 for 4" do | |
expect(4.to_fizzbuzz).to eq 4 | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment