From: Christian Roese Date: 2007-06-04T01:26:01+09:00 Subject: [QUIZ] FizzBuzz (#126) ------=_Part_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: ====CODE 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 = 1, 100, 3, 5, 15 START.upto(STOP) do |n| result = if (n % BOTH == 0) then "FizzBuzz" elsif (n % FIZZ == 0) then "Fizz" elsif (n % BUZZ == 0) then "Buzz" else n end puts result end ====CODE END==== ========================================== After seeing the post about making the core loop (1..100).each { |x| p x } I came up with this little ditty: ====CODE 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 = 3, 5, 15 def inspect result = if (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 } ====CODE END==== Not too shabby for only a couple weeks of using Ruby! I love this language!! -- Christian Roese ------=_Part_16487_13372481.1180887958663--