Created
November 19, 2011 06:15
-
-
Save pasberth/1378521 to your computer and use it in GitHub Desktop.
alias xxx_original_require require みたいなことをしても済むように作った
This file contains hidden or 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 Module | |
| def original method | |
| original_methods = Hash.new do |original_methods, method| | |
| original_methods[method.to_sym] = original_method = [] | |
| define_method :"original_#{method.to_sym}" do |*args, &blk| | |
| fail if original_method.empty? | |
| tmp = original_method.pop | |
| res = tmp.call *args, &blk | |
| original_method.push tmp | |
| res | |
| end | |
| original_method | |
| end | |
| define_method :original do |method| | |
| original_methods[method.to_sym].push self.method(method.to_sym) | |
| end | |
| original method | |
| end | |
| def override method, &definition | |
| original method | |
| define_method method, &definition | |
| end | |
| end | |
| module Test | |
| extend self | |
| def hello | |
| puts "hello" | |
| end | |
| original :hello | |
| def hello | |
| original_hello | |
| puts "also" | |
| end | |
| original :hello | |
| def hello | |
| original_hello | |
| puts "oooooo" | |
| end | |
| override :hello do | |
| original_hello | |
| puts "aaaa" | |
| end | |
| end | |
| Test.hello | |
| # stdout | |
| # hello | |
| # also | |
| # oooooo | |
| # aaaa | |
| module Kernel | |
| original :require # => ただしこれがなぜかSystemStackErrorになる | |
| def require path | |
| puts "override require!" | |
| original_require path | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment