------ art_16487_13372481.1180887958663
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Great quiz, really got me thinking about other ways to solve this
simple problem, but I settled on this clear and easy method.
Anyway, here we go:
E BEGIN !/usr/bin/ruby -w
###########################
# Filename: fizzbuzz.rb
# Author: Christian Roese
# Date: 2007-06-01
###########################
# Ruby Quiz - FizzBuzz (#126)
# This program will print out the numbers from 1 to 100,
# replacing multiples of 3 with "Fizz", the multiples of
# 5 with "Buzz", and the multiples of 3 and 5 with "FizzBuzz"
# I love me some constants!
START, STOP, FIZZ, BUZZ, BOTH , 100, 3, 5, 15
START.upto(STOP) do |n|
result f (n % BOTH 0) then "FizzBuzz"
elsif (n % FIZZ 0) then "Fizz"
elsif (n % BUZZ 0) then "Buzz"
else n
end
puts result
end
¥¿DE END
After seeing the post about making the core loop
(1..100).each { |x| p x }
I came up with this little ditty:
¥¿DE BEGIN !/usr/bin/ruby -w
###########################
# Filename: fizzbuzz2.rb
# Author: Christian Roese
# Date: 2007-06-01
###########################
# change the way Fixnum's spit out their value
class Fixnum
FIZZ, BUZZ, BOTH , 5, 15
def inspect
result f (self % BOTH 0) then "FizzBuzz"
elsif (self % FIZZ 0) then "Fizz"
elsif (self % BUZZ 0) then "Buzz"
else self
end
end
end
# main loop
(1..100).each { |x| p x }
¥¿DE END ot too shabby for only a couple weeks of using Ruby!
I love this language!!
--
Christian Roese
------ art_16487_13372481.1180887958663--