La verdad es que es bastante divertido...
   
  dos soluciones:
   
  una agregando metodos a FixNum
   
  #!/bin/env ruby
  class Fixnum
 def divisible_by?(n)
  ((self % n) == 0) 
 end
   def fizz
  divisible_by?(3)?"Fizz":""
 end
   def buzz
  divisible_by?(5)?"Buzz":""
 end
   def fizzbuzzeable?
  divisible_by?(3) || divisible_by?(5) 
 end
   def fizzbuzz
  fizzbuzzeable? ? (fizz + buzz) : self
 end 
  end
  
(1..100).each do |n|
 printf("%s\n",n.fizzbuzz)
end

  y otra con una tabla 
   
  #!/bin/env ruby
  tabla = {
3 => "Fizz",
5 => "Buzz",
6 => "Fizz",
9 => "Fizz",
10 => "Buzz",
12 => "Fizz",
0 => "FizzBuzz",
}
  (1..100).each do |n|
 printf("%s\n",tabla[n % 15] || sprintf("%d",n))
end

   
  Saludos!
   
   
   
  

Aureliano Calvo <[EMAIL PROTECTED]> escribió:
  A mi mucho los one-liners no me gustan, así que escribí algo más largo y 
(IMNSHO) claro. En particular me parece que quedó más legible poniendo los if y 
unless a la derecha.

#!/usr/bin/env ruby

(1..100).each do 
  |n|
  div_by_3 = ( n % 3 ) == 0
  div_by_5 = ( n % 5 ) == 0
  print "Fizz" if div_by_3
  print "Buzz" if div_by_5
  print n unless ( div_by_3 or div_by_5 )
end


> "Write a program that prints the numbers from 1 to 100. But for multiples of 
> three print "Fizz" instead of the number and for the multiples of five print 
> "Buzz". For numbers which are multiples of both three and five print 
> "FizzBuzz"." _______________________________________________
ruby mailing list
[email protected]
http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar


 __________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! 
Regístrate ya - http://correo.espanol.yahoo.com/ 
_______________________________________________
ruby mailing list
[email protected]
http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar

Responder a