Last active
August 29, 2015 14:07
-
-
Save Jell/d94f73f97b2968e8c496 to your computer and use it in GitHub Desktop.
FizzBuzz qualified constants
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 'active_support/core_ext' | |
module T | |
module T | |
module T | |
def self.zero; end | |
end | |
end | |
end | |
module F | |
module F | |
module F | |
module F | |
module F | |
def self.zero; end | |
end | |
end | |
end | |
end | |
end | |
def fizz_buzz (n) | |
mod3 = Object.qualified_const_get((["T"]*n).join("::")) | |
mod5 = Object.qualified_const_get((["F"]*n).join("::")) | |
if mod3.respond_to?(:zero) && mod5.respond_to?(:zero) | |
"FizzBuzz" | |
elsif mod3.respond_to?(:zero) | |
"Fizz" | |
elsif mod5.respond_to?(:zero) | |
"Buzz" | |
else | |
n | |
end | |
end | |
(1..20).map(&method(:fizz_buzz)) | |
#=> [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz", 16, 17, "Fizz", 19, "Buzz"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment