I want to write small class with some methods, which actualy belongs to other classes, so how can I define methods in other classes which are copies of existing. I believe it is metaprogramming magi I don't understand.
class Foo
def initialize
# with blocks, I would just pass block, but this is methods
# so this won't work
Bar.class_eval(perform)
Bar.class_eval(process)
Bar.class_eval(save)
end
def perform
1+1
end
def process
# some code
end
def save
# some code
end
end
class Bar; end
foo = Foo.new
foo.perform
#=> 2
Bar.test
#=> 1
Why I need this? I am working on gem which takes a class with just three methods. On initializing (which ill be hidden in parent class) it will pass this methods to different classes. I can make this with blocks, but with methods it is little cleaner.
PS: It is like copying methods from one class to another
PSS: Or... how to convert method to proc, so I can pass it to class_eval
To convert a method to something which can be called like a Proc, use obj.method(:method_name)
. That will give you a bound Method object, which when call
ed, will be invoked on obj
. If you want to invoke it on a different object of the same class, you can call method.unbind.bind(different_obj)
.
That still doesn't allow you to "copy" methods from one class to another. If you want to allow the user to pass a class which defines 3 methods, rather than passing 3 blocks, it might work better if you store a reference to that class (or an instance of it) internally, and call methods on it as required. That's what the person who commented about "delegation" meant.
OR, you can let the user pass a Module, and make your own class include
or extend
the module (as required).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With