I've been out of the loop, but love this game, here is my hopefully readable one line Ruby version.
1.upto(100){|num| string=“#{'Fizz' if num % 3 == 0}#{'Buzz' if num % 5 == 0}”; puts string.empty? num : string }
1.upto(100){|num| string=“#{'Fizz' if num % 3 == 0}#{'Buzz' if num % 5 == 0}”; puts string.empty? num : string }
Is that executable code inside the string? That's kind of cool in a “wanting to look at a road crash” sort of way. I do love the Smalltalky goodness of those blocks.
You are being quite naughty though by not even running this stuff :) Note the extra “?” you need.
1.upto(100){|num| string=“#{'Fizz' if num % 3 == 0}#{'Buzz' if num % 5 == 0}”; puts string.empty? ? num : string }
How would you pass in a lambda so that “puts” is pluggable in Ruby? There's also the configurable rules — that would be neat to see in Ruby.
Is that executable code inside the string? That's kind of cool in a “wanting to look at a road crash” sort of way. I do love the Smalltalky goodness of those blocks.
You are being quite naughty though by not even running this stuff :) Note the extra “?” you need.
1.upto(100){|num| string=“#{'Fizz' if num % 3 == 0}#{'Buzz' if num % 5 == 0}”; puts string.empty? ? num : string }
How would you pass in a lambda so that “puts” is pluggable in Ruby? There's also the configurable rules — that would be neat to see in Ruby.
Executable code inside strings construction rocks! I use it all the time. There is no risk since you are not executing the string, its just code that helps you constuct the string. Saves you having to do lots of yak shaving to build up strings.
As for making it configurable and what not..
def fizzBuzz(min=1, max=100, param={3=>'Fizz', 5=>'Buzz'}) min.upto(max){|num| string=param.collect{|k,v| v if num%k==0}*''; yield(string.empty? ? num : string) } end
fizzBuzz{|x| puts x}
fizzBuzz(50, 200, 3=>'Fizz', 5=>'Buzz', 6=>'Bang'){|x| `say #{x}`}
Put that in your pipe and smoke it.