Last active
December 14, 2015 04:29
-
-
Save hanachin/5028247 to your computer and use it in GitHub Desktop.
FizzBuzz in ruby 2.0
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
#!/usr/bin/env ruby | |
class FizzBuzz | |
include Enumerable | |
def label(n) | |
@label_definitions.lazy.select {|label, denominator| | |
n % denominator == 0 | |
}.map(&:first).first | |
end | |
def fizzbuzz(n) | |
label(n) || n | |
end | |
def initialize(label_definitions = {fizzbuzz: 15, fizz: 3, buzz: 5}) | |
@label_definitions = label_definitions | |
end | |
def each | |
(1..Float::INFINITY).lazy.each {|n| | |
yield fizzbuzz(n) | |
} | |
end | |
end | |
limit, fizz, buzz = ARGV.map(&:to_i) | |
limit ||= 100 | |
fizz ||= 3 | |
buzz ||= 5 | |
puts FizzBuzz.new(fizzbuzz: fizz.lcm(buzz), fizz: fizz, buzz: buzz).take(limit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
キーワード引数使って
してもいいかもしれないですね。