hp12c
24 March 2010

Yet Another Ruby FizzBuzz その4

Yet Another Ruby FizzBuzz その4

class Fixnum
 def fizzbuzz
 w = {Fizz: 3, Buzz: 5}.select { |_, base| (self%base).zero? }
 w.empty? ? self : w.keys.join
 end
end
(1..100).each { |i| print "#{i.fizzbuzz} " }

Enumerable#injectのほうがいいかな

class Fixnum
 def fizzbuzz
 w = {Fizz: 3, Buzz: 5}.inject("") { |mem, (word, base)| (self%base).zero? ? mem << word : mem }
 w.empty? ? self : w
 end
end
(1..100).each { |i| print "#{i.fizzbuzz} " }

Yet Another Ruby FizzBuzz その3

divisible_by = lambda { |base, num| (num % base).zero? }.curry
divisible_by_15 = divisible_by[15]
divisible_by_3 = divisible_by[3]
divisible_by_5 = divisible_by[5]
(1..100).each do |i|
 puts case i
 when divisible_by_15 then 'FizzBuzz'
 when divisible_by_3 then 'Fizz'
 when divisible_by_5 then 'Buzz'
 else i
 end
end

Yet Another Ruby FizzBuzz その2

class Fixnum
 def self.fizzbuzzize
 alias org_to_s to_s
 def to_s
 if self%15 == 0 then "FizzBuzz"
 elsif self%3 == 0 then "Fizz"
 elsif self%5 == 0 then "Buzz"
 else self.org_to_s
 end
 end
 end
 def self.unfizzbuzzize
 undef to_s
 alias to_s org_to_s
 end
end
Fixnum.fizzbuzzize
(1..100).each { |i| print "#{i} " }
puts
Fixnum.unfizzbuzzize
(1..100).each { |i| print "#{i} " }

Yet Another Ruby FizzBuzz



Please enable JavaScript to view the comments powered by Disqus. blog comments powered by Disqus
ruby_pack8

100円〜で好評発売中!
M'ELBORNE BOOKS


AltStyle によって変換されたページ (->オリジナル) /