如何在模块中为类方法添加别名? [英] How to alias a class method within a module?

查看:116
本文介绍了如何在模块中为类方法添加别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ruby v1.9.2和Ruby on Rails v3.2.2 gem.我有以下模块

I am using Ruby v1.9.2 and the Ruby on Rails v3.2.2 gem. I had the following module

module MyModule
  extend ActiveSupport::Concern

  included do
    def self.my_method(arg1, arg2)
      ...
    end
  end
end

,我想为 class方法 my_method加上别名.因此,我声明了以下(不起作用)代码:

and I wanted to alias the class method my_method. So, I stated the following (not working) code:

module MyModule
  extend ActiveSupport::Concern

  included do
    def self.my_method(arg1, arg2)
      ...
    end

    # Note: the following code doesn't work (it raises "NameError: undefined
    # local variable or method `new_name' for #<Class:0x00000101412b00>").
    def self.alias_class_method(new_name, old_name)
      class << self
        alias_method new_name, old_name
      end
    end

    alias_class_method :my_new_method, :my_method
  end
end

换句话说,我想以某种方式扩展Module类,以便添加整个MyModule中可用的alias_class_method方法.但是,我想让它在我所有的Ruby on Rails应用程序中都可以正常工作并可以使用.

In other words, I thought to extend the Module class someway in order to add an alias_class_method method available throughout MyModule. However, I would like to make it to work and to be available in all my Ruby on Rails application.

  1. 我应该将与Module类的Ruby核心扩展有关的文件放在哪里?也许在Ruby on Rails lib目录中?
  2. 我应该如何在核心扩展文件中适当地扩展" Module类?
  3. 这是正确的进行方式吗?也就是说,例如,我应该扩展"另一个类(ObjectBasicObjectKernel,...)而不是"Module"吗?还是应该完全避免实施上述核心扩展?
  1. Where I should put the file related to the Ruby core extension of the Module class? Maybe in the Ruby on Rails lib directory?
  2. How should I properly "extend" the Module class in the core extension file?
  3. Is it the right way to proceed? That is, for example, should I "extend" another class (Object, BasicObject, Kernel, ...) rather than Module? or, should I avoid implementing the mentioned core extension at all?

但是,更重要,是否有一个Ruby功能可以使我尝试完成的工作,而不必扩展其类?

But, more important, is there a Ruby feature that makes what I am trying to accomplish so that I don't have to extend its classes?

推荐答案

您可以使用define_singleton_method将旧方法换成新名称,如下所示:

You could use define_singleton_method to wrap your old method under a new name, like so:

module MyModule
  def alias_class_method(new_name, old_name)
    define_singleton_method(new_name) { old_name }
  end
end

class MyClass
  def my_method
    puts "my method"
  end
end

MyClass.extend(MyModule)
MyClass.alias_class_method(:my_new_method, :my_method)
MyClass.my_new_method     # => "my method"

回答您的评论,您不必手动扩展每个课程. define_singleton_methodObject类中实现.因此,您可以简单地扩展Object类,因此每个类都应具有可用的方法...

Answering your comment, you wouldn't have to extend every single class by hand. The define_singleton_method is implemented in the Object class. So you could simply extend the Object class, so every class should have the method available...

Object.extend(MyModule)

将其放入Rails应用程序的初始化程序中,您应该会很好...

Put this in an initializer in your Rails app and you should be good to go...

这篇关于如何在模块中为类方法添加别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆