alias_method and class_methods don't mix?

前端 未结 1 1185
自闭症患者
自闭症患者 2021-02-05 15:36

I\'ve been trying to tinker with a global Cache module, but I can\'t figure out why this isn\'t working.

Does anyone have any suggestions?

This is the error:

1条回答
  •  一整个雨季
    2021-02-05 16:14

    The calls to alias_method will attempt to operate on instance methods. There is no instance method named get in your Cache module, so it fails.

    Because you want to alias class methods (methods on the metaclass of Cache), you would have to do something like:

    class << Cache  # Change context to metaclass of Cache
      alias_method :get_not_modified, :get
      alias_method :get, :get_modified
    end
    
    Cache.get
    
    class << Cache  # Change context to metaclass of Cache
      alias_method :get, :get_not_modified
    end
    

    0 讨论(0)
提交回复
热议问题