One Line Ruby Version Luke 21st July, 2008 08:56 (UTC)

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 }

One Line Ruby Version Kirit Sælensminde 21st July, 2008 09:29 (UTC)
Luke said

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.


To join in the discussion you should register or log in
One Line Ruby Version Luke 21st July, 2008 10:08 (UTC)

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

  1. Same as above, just your standard.

fizzBuzz{|x| puts x}

  1. Only works on osx, will speak it out loud

fizzBuzz(50, 200, 3=>'Fizz', 5=>'Buzz', 6=>'Bang'){|x| `say #{x}`}

Put that in your pipe and smoke it.

One Line Ruby Version Luke 21st July, 2008 10:21 (UTC)
Both method and call can be squeezed to fit in 124 chars (including new line) def fb(a,b,p)a.upto(b){|n|s=p.collect{|k,v|v if n%k==0}*;yield(s== ? n:s)};end fb(1,100,3=>'Fizz',5=>'Buzz'){|x|puts x}
One Line Ruby Version Luke 21st July, 2008 10:24 (UTC)
You can also cheat. require 'rubygems' require 'fizzbuzz' puts fizzbuzz