Last active
September 4, 2015 07:15
-
-
Save jonatas/284065 to your computer and use it in GitHub Desktop.
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
module Delegate | |
def self.included(base) | |
base.class_eval do | |
def method_missing(name, *args, &block) | |
self.send(@@delegators[name]).send name, *args, &block | |
end | |
class << self | |
def delegate method, options | |
(@@delegators ||= {})[method] = options[:to] | |
end | |
end | |
end | |
end | |
end | |
include Delegate | |
class A | |
attr_accessor :b | |
delegate :c, :to => :b | |
end | |
class B | |
def c | |
puts "c from #{self.class}" | |
end | |
end | |
a = A.new | |
a.b = B.new | |
a.c | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment