Created
May 12, 2020 13:44
-
-
Save tsaito-cyber/30ce47b5d99fa6f13982d228572b2da2 to your computer and use it in GitHub Desktop.
delegatable.rb
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 Delegatable | |
def self.included(base) | |
base.class_eval do | |
def self.delegate_method(method, to) | |
define_method(method) do |*args| | |
instance_variable_get(to).__send__(method, *args) | |
end | |
end | |
end | |
end | |
end | |
class X | |
include Delegatable | |
delegate_method :push, :@que | |
attr_accessor :que | |
def initialize | |
@que = [] | |
end | |
end | |
x = X.new | |
x.push 1, 2, 3 | |
p x.que | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment