Skip to content

Instantly share code, notes, and snippets.

@shugo
Created August 10, 2012 04:45
Show Gist options
  • Save shugo/3311127 to your computer and use it in GitHub Desktop.
Save shugo/3311127 to your computer and use it in GitHub Desktop.
@yhara こんな感じ
module FizzBuzzInteger
refine Fixnum do
def to_s
self % 15 == 0 ? "FizzBuzz" :
self % 5 == 0 ? "Fizz" :
self % 3 == 0 ? "Buzz" :
super
end
end
end
class A
using FizzBuzzInteger
def foo
p 100.to_s
#=> "Fizz"
p (1..10).map(&:to_s)
#=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] # lexical scoping
p (1..10).map{|x| x.to_s}
#=> ["1", "2", "Buzz", "4", "Fizz", "Buzz", "7", "8", "Buzz", "Fizz"]
end
end
A.new.foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment