Skip to content

Instantly share code, notes, and snippets.

@rosylilly
Created January 5, 2014 07:33
Show Gist options
  • Save rosylilly/8265512 to your computer and use it in GitHub Desktop.
Save rosylilly/8265512 to your computer and use it in GitHub Desktop.
refinements w/ block
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
@rosylilly
Copy link
Author

  1. refine(klass) { } で空のブロックを渡しているのはブロックなしで実行できないため
  2. refine(klass, &block) は処理系で規制されておりつかえない
  3. alias_method でも十分にたような別名が使える

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment