Skip to content

Instantly share code, notes, and snippets.

@404pnf
Forked from plexus/fizz_buzz.rb
Created June 5, 2014 00:58
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 404pnf/2d4d1a72e095a193e688 to your computer and use it in GitHub Desktop.
Save 404pnf/2d4d1a72e095a193e688 to your computer and use it in GitHub Desktop.
class FizzBuzz
def fizz
loop do
["", "", "Fizz"].each{|s| yield s}
end
end
def buzz
loop do
["", "", "", "", "Buzz"].each{|s| yield s}
end
end
def fizzbuzz
return to_enum(:fizzbuzz) unless block_given?
fizz = to_enum(:fizz)
buzz = to_enum(:buzz)
(1..Float::INFINITY).each do |i|
fizzbuzz = [fizz.next, buzz.next].join
yield fizzbuzz.empty? ? i : fizzbuzz
end
end
end
puts FizzBuzz.new.fizzbuzz.take(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment