Skip to content

Instantly share code, notes, and snippets.

@moguno
Created July 6, 2014 04:06
Show Gist options
  • Save moguno/3efa856a541b6ee61230 to your computer and use it in GitHub Desktop.
Save moguno/3efa856a541b6ee61230 to your computer and use it in GitHub Desktop.
Rubyでこんな事まで出来て、思わず今の地位とか家庭とかかなぐり捨てて島根に移住しそうになった。
# スーパークラスを維持しつつ、委譲を行う。
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