Rails 怎么正确的重写 lib 中定义的 module 内的方法?

luoguanzhong · September 22, 2014 · Last by luoguanzhong replied at September 27, 2014 · 2015 hits

我想用 class_eval 重写 lib 文件夹中,定义的 method 中的类中的方法,

lib/a.rb,文件:

module A
   class B
     def mm
     end
   end
  end
end

请问各位,下面的重写类方法的 code 放在哪里啊?不胜感谢!!

A::B.class_eval do 
  alias_method :old_mm, mm
  def mm
  end
end

你的写法是对的,不过 alias_methd 方法的第二个参数错了,要用 Symbol。 你写成了一个变量。 alias_method :old_mm, :mm

lib 是你的,直接写在 lib 里面就行了啊。不到穷尽所有其他方法,绝不能写这样的代码。

@filetype @billy ,多谢,最终写在 initializer 中了。

You need to Sign in before reply, if you don't have an account, please Sign up first.