Created
August 30, 2019 00:46
-
-
Save JonathanWThom/8f55730450507e6da7329ce6991d750d to your computer and use it in GitHub Desktop.
Prompt: What is the most complex and unreadable way you can write FizzBuzz?
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
(1..100).each do |i| | |
[3, 5].each do |num| | |
define_method("klass_#{num}") do | |
Class.new do | |
if i % num == 0 | |
define_method("#{i}") do | |
call | |
end | |
end | |
def call | |
self.class.name | |
end | |
end | |
end | |
end | |
fizz = Object.const_set(:Fizz, klass_3) | |
buzz = Object.const_set(:Buzz, klass_5) | |
result = [fizz.new, buzz.new].map do |klass| | |
begin | |
klass.send("#{i}") | |
rescue | |
end | |
end.compact | |
if result.empty? | |
puts i | |
else | |
puts result&.join("") | |
end | |
Object.send(:remove_const, :Fizz) | |
Object.send(:remove_const, :Buzz) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment