Skip to content

Instantly share code, notes, and snippets.

@bangn
Last active September 15, 2017 11:19
Show Gist options
  • Save bangn/93524dcb60e0849c04e5e653bb628921 to your computer and use it in GitHub Desktop.
Save bangn/93524dcb60e0849c04e5e653bb628921 to your computer and use it in GitHub Desktop.
Copy a very clever fizzbuzz implementation in ruby. http://dave.fayr.am/posts/2012-10-4-finding-fizzbuzz.html
class FizzBuzz
def initialize
@printed = nil
@printers = [
lambda {|x| print (@printed = "Fizz") if x % 3 == 0},
lambda {|x| print (@printed = "Buzz") if x % 5 == 0},
lambda {|x| print (@printed = "Bazz") if x % 7 == 0}
]
end
def for_num(i)
@printed = nil
@printers.each { |l| l.call(i) }
print i unless @printed
puts # Newline
end
def run(x = 100)
(1..x).to_a.each { |i| for_num(i) }
end
end
# Add option to count to values > 100 since 3 * 5 * 7 > 100
FizzBuzz.new.run(ARGV.first || 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment