--Boundary-00=_dZ3YGrWwbOouLYM
Content-Type: Multipart/Mixed;
boundary="Boundary-00=_dZ3YGrWwbOouLYM"
--Boundary-00=_dZ3YGrWwbOouLYM
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Attached are my 7 attempts. Some are ok, some are crap. No golfing.
--
Jesse Merriman
(e-mail address removed)
http://www.jessemerriman.com/
--Boundary-00=_dZ3YGrWwbOouLYM
Content-Type: application/x-ruby;
name="fb_0.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="fb_0.rb"
#!/usr/bin/env ruby
# Ruby Quiz 126: FizzBuzz
# fb_0.rb
Fizz = 3
Buzz = 5
FizzBuzz = Fizz * Buzz
(1..100).each do |x|
if (x % FizzBuzz).zero? then puts 'FizzBuzz'
elsif (x % Fizz).zero? then puts 'Fizz'
elsif (x % Buzz).zero? then puts 'Buzz'
else puts x
end
end
--Boundary-00=_dZ3YGrWwbOouLYM
Content-Type: application/x-ruby;
name="fb_4.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="fb_4.rb"
#!/usr/bin/env ruby
# Ruby Quiz 126: FizzBuzz
# fb_4.rb
Fizz = 3
Buzz = 5
FizzBuzz = Fizz * Buzz
class Integer
def fizzbuzz
if (self % FizzBuzz).zero? then 'FizzBuzz'
elsif (self % Fizz).zero? then 'Fizz'
elsif (self % Buzz).zero? then 'Buzz'
else to_s
end
end
end
(1..100).each { |x| puts x.fizzbuzz }
--Boundary-00=_dZ3YGrWwbOouLYM
Content-Type: application/x-ruby;
name="fb_2.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="fb_2.rb"
#!/usr/bin/env ruby
# Ruby Quiz 126: FizzBuzz
# fb_2.rb
Fizz = 3
Buzz = 5
arr = Array.new(100) do |i|
x = i + 1
if (x % Fizz).zero?
if (x % Buzz).zero? then 'FizzBuzz'
else 'Fizz' end
elsif (x % Buzz).zero? then 'Buzz'
else x end
end
arr.each { |e| puts e }
--Boundary-00=_dZ3YGrWwbOouLYM
Content-Type: application/x-ruby;
name="fb_1.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="fb_1.rb"
#!/usr/bin/env ruby
# Ruby Quiz 126: FizzBuzz
# fb_1.rb
Fizz = 3
Buzz = 5
(1..100).each do |x|
if (x % Fizz).zero?
if (x % Buzz).zero? then puts 'FizzBuzz'
else puts 'Fizz' end
elsif (x % Buzz).zero? then puts 'Buzz'
else puts x end
end
--Boundary-00=_dZ3YGrWwbOouLYM
Content-Type: application/x-ruby;
name="fb_5.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="fb_5.rb"
#!/usr/bin/env ruby
# Ruby Quiz 126: FizzBuzz
# fb_5.rb
class Integer
def fizzbuzz arr, str
arr.each_with_index do |x, i|
arr
= ((i+1) % self).zero? ? str : x
end
end
end
arr = (1..100).to_a
3.fizzbuzz arr, 'Fizz'
5.fizzbuzz arr, 'Buzz'
15.fizzbuzz arr, 'FizzBuzz' # Must be last.
arr.each { |x| puts x }
--Boundary-00=_dZ3YGrWwbOouLYM
Content-Type: application/x-ruby;
name="fb_6.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="fb_6.rb"
#!/usr/bin/env ruby
# Ruby Quiz 126: FizzBuzz
# fb_6.rb
FB = lambda { |mod, str| lambda { |x| (x % mod).zero? ? str : x } }
Fizz = FB[3, 'Fizz']
Buzz = FB[5, 'Buzz']
FizzBuzz = FB[15, 'FizzBuzz']
# Note that this wouldn't work if the numbers to_s'd got longer than their
# fizz-buzzed strings.
(1..100).each do |x|
puts([Fizz[x], Buzz[x], FizzBuzz[x]].max do |a,b|
a.to_s.length <=> b.to_s.length
end)
end
--Boundary-00=_dZ3YGrWwbOouLYM
Content-Type: application/x-ruby;
name="fb_3.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="fb_3.rb"
#!/usr/bin/env ruby
# Ruby Quiz 126: FizzBuzz
# fb_3.rb
Fizz = 3
Buzz = 5
FizzBuzz = Fizz * Buzz
class Fixnum
alias ld_to_s :to_s
def to_s; (self % FizzBuzz).zero? ? 'FizzBuzz' :
(self % Fizz).zero? ? 'Fizz' :
(self % Buzz).zero? ? 'Buzz' :
old_to_s
end
end
# Peter Seebach's suggestion.
(1..100).each { |x| p x }
--Boundary-00=_dZ3YGrWwbOouLYM--
--Boundary-00=_dZ3YGrWwbOouLYM--