Created
January 5, 2014 07:33
-
-
Save rosylilly/8265512 to your computer and use it in GitHub Desktop.
refinements w/ block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
def say | |
puts "A" | |
end | |
end | |
module B | |
def self.role(klass, &block) | |
ref = refine(klass) { } | |
ref.module_eval(&block) | |
end | |
role A do | |
def say | |
puts "B" | |
end | |
end | |
end | |
class C | |
using B | |
def say | |
A.new.say | |
end | |
end | |
A.new.say # => A | |
C.new.say # => B |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
refine(klass) { }
で空のブロックを渡しているのはブロックなしで実行できないためrefine(klass, &block)
は処理系で規制されておりつかえないalias_method
でも十分にたような別名が使える