Skip to content

Instantly share code, notes, and snippets.

@jonatas
Last active September 4, 2015 07:15
Show Gist options
  • Save jonatas/284065 to your computer and use it in GitHub Desktop.
Save jonatas/284065 to your computer and use it in GitHub Desktop.
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