Created
July 6, 2014 04:06
-
-
Save moguno/3efa856a541b6ee61230 to your computer and use it in GitHub Desktop.
Rubyでこんな事まで出来て、思わず今の地位とか家庭とかかなぐり捨てて島根に移住しそうになった。
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
# スーパークラスを維持しつつ、委譲を行う。 | |
def get_delegated_instance(base_instance) | |
Class.new(base_instance.class) { |klass| | |
@base_instance = nil | |
# 取りあえずインスタンスメソッドを全部移譲先に転送する。 | |
self.instance_methods.each { |method| | |
define_method(method) { |*args| | |
@base_instance.send(method, *args) | |
} | |
} | |
def initialize(base_instance) | |
@base_instance = base_instance | |
end | |
}.new(base_instance) | |
end | |
puts get_delegated_instance([32, 1, 2, 3])[2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment