Skip to content

Instantly share code, notes, and snippets.

@Jell
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jell/d94f73f97b2968e8c496 to your computer and use it in GitHub Desktop.
Save Jell/d94f73f97b2968e8c496 to your computer and use it in GitHub Desktop.
FizzBuzz qualified constants
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