From: Sharon Phillips Date: 2007-06-03T21:14:27+09:00 Subject: Re: [QUIZ] FizzBuzz (#126) Well, It's a few minutes early, but I've got an early morning tomorrow. Here's three attempts from me. First is my initial shot, which will cause many of you to cringe at the use of nested ternary operators... Second, I decided to mess with Fixnum. Then I got a little bored and thought of something for our friends at thedailywtf (still can't take to the new name) I'm willing to go with whoever suggested this might bring in the largest number of RubyQuiz responses yet. Cheers, Dave #------------------------------------------------- #Quick first attempt 1.upto(100) {|i| puts(i%15==0 ? 'FizzBuzz' : i%5==0 ? 'Buzz' : i% 3==0 ? 'Fizz' : i)} #------------------------------------------------- #Lets play with Fixnum class Fixnum def fizz_buzzed a= (self%3==0 ? 'Fizz' : "") a+= 'Buzz' if self%5==0 a= self.to_s if a=="" a end end 1.upto(100) {|i| puts i.fizz_buzzed} #------------------------------------------------- # How about something for thedailywtf.com? def divisible_by_3(i) if i.to_s.size>1 then divisible_by_3(i.to_s.split(//).map{|char| char.to_i}.inject{| a,b| a+b}) else [3,6,9].include?(i) end end def divisible_by_5(i) ['0','5'].include?(i.to_s[-1].chr) end def divisible_by_15(i) divisible_by_3(i) && divisible_by_5(i) end 1..100.each do |i| if divisible_by_15(i) then puts 'FizzBuzz' elsif divisible_by_3(i) then puts 'Fizz' elsif divisible_by_5(i) then puts 'Buzz' else puts i end end