To better understand meta-programming in Ruby I would like to make an object, when instantiated, delegate all methods of an object passed as a parameter. And your class will delegate the methods that the class contains. However, I would like Object and Class methods, such as: send and object_id, which can cause errors if changed,
I thought of something like:
def delegate_all
delegate_instance if !@delegated
delegate_singleton if !@@delegated
end
def delegate_instance
((@model.class.instance_methods-Object.instance_methods)-Class.instance_methods).each do |name|
name = name.to_sym
puts name.to_s
self.class.send(:define_method, name){ |*args|
method = @model.method name
#if method.arity > 0
method.call args
#else
# method.call
#end
}
end
@delegated = true
end
def delegate_singleton
((@model.class.singleton_methods-Class.singleton_methods)-Object.singleton_methods).each do |name|
name = name.to_sym
puts name.to_s
self.class.define_singleton_method(name){|*args|
name = name.to_sym
method = @model.singleton_method name
#if method.arity > 0
method.call args
#else
# method.call
#end
}
end
@@delegated = true
end
Does anyone have any idea how I can do this ?, but I should also consider methods with parameters.