将方法从一个类复制到另一个类

时间:2021-04-09 16:04:54

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.

为什么我需要这个?我正在研究gem,它只需要三个方法。在初始化时(可能隐藏在父类中),它会将此方法传递给不同的类。我可以使用块来制作它,但是使用方法它会更清洁。

PS: It is like copying methods from one class to another

PS:这就像从一个类复制方法到另一个类

PSS: Or... how to convert method to proc, so I can pass it to class_eval

PSS:或者...如何将方法转换为proc,所以我可以将它传递给class_eval

1 个解决方案

#1


12  

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 called, 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).

要将方法转换为可以像Proc一样调用的方法,请使用obj.method(:method_name)。这将为您提供一个绑定的Method对象,该对象在调用时将在obj上调用。如果要在同一个类的不同对象上调用它,可以调用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.

这仍然不允许您将方法从一个类“复制”到另一个类。如果您希望允许用户传递一个定义3个方法的类,而不是传递3个块,那么如果在内部存储对该类(或其实例)的引用,并根据需要调用其上的方法,它可能会更好。那就是评论“授权”的人的意思。

OR, you can let the user pass a Module, and make your own class include or extend the module (as required).

或者,您可以让用户传递一个Module,并使您自己的类包含或扩展模块(根据需要)。

#1


12  

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 called, 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).

要将方法转换为可以像Proc一样调用的方法,请使用obj.method(:method_name)。这将为您提供一个绑定的Method对象,该对象在调用时将在obj上调用。如果要在同一个类的不同对象上调用它,可以调用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.

这仍然不允许您将方法从一个类“复制”到另一个类。如果您希望允许用户传递一个定义3个方法的类,而不是传递3个块,那么如果在内部存储对该类(或其实例)的引用,并根据需要调用其上的方法,它可能会更好。那就是评论“授权”的人的意思。

OR, you can let the user pass a Module, and make your own class include or extend the module (as required).

或者,您可以让用户传递一个Module,并使您自己的类包含或扩展模块(根据需要)。