Ruby反射组合:从重新定义的方法调用原始方法

时间:2016-05-08 09:05:33

标签: ruby reflection cop

首先是一些上下文

我有一个类Phone,它定义了一个方法advertise,如下所示:

class Phone
  def advertise(phone_call)
    'ringtone'
  end
end

我想对这种方法进行一些调整。 例如,当用户处于安静的环境中时,手机应振动而不是响铃。 为此,我定义了像

这样的模块
module DiscreetPhone    
  def advertise_quietly (phone_call)
    'vibrator'
  end
end

然后我的程序就可以了

# add the module to the class so that we can redefine the method
Phone.include(DiscreetPhone) 
# redefine the method with its adaptation
Phone.send(:define_method, :advertise, DiscreetPhone.instance_method(:advertise_quietly ))

当然,对于这个例子,我硬编码了类和模块的名称,但它们应该是函数的参数。

因此,执行示例将给出:

phone = Phone.new
phone.advertise(a_call) # -> 'ringtone'
# do some adaptation stuff to redefine the method
...
phone.advertise(a_call) # -> 'vibrator'

最后回答我的问题

我希望有一个适应调用原始函数并在其结果中添加一些内容。我想把它写成

module ScreeningPhone
  def advertise_with_screening (phone_call)
    proceed + ' with screening'
  end
end

但我不知道proceed调用应该做什么,甚至不知道应该在哪里定义它。

  • 我在Windows上使用Ruby 2.3.0。
  • proceed可以用其他东西替换,但我想在定义改编的模块中尽可能保持干净。

2 个答案:

答案 0 :(得分:1)

您可以通过prepending模块执行此操作,而不是将其包括在内。

不要将define_method用作ersatz alias_method,而只需在模块中调用方法advertise

advertise方法中,您可以调用super来调用继承层次结构。

答案 1 :(得分:0)

在我看来,这种方法过于复杂,并且对Module的使用不恰当。

我建议考虑一种更简单的方法来实现它。

一种简单的方法是在Phone类中包含所有方法。

或者,您可以使用哈希作为环路策略的查找表:

class Phone

    attr_accessor :ring_strategy

    RING_STRATEGIES = {
        ringtone:  -> { ring_with_tone },
        discreet:  -> { ring_quietly },
        screening: -> { ring_with_tone; ring_screening_too }
        # ...
    }

    def initialize(ring_strategy = :ringtone)
        @ring_strategy = ring_strategy
    end

    def ring
        RING_STRATEGIES[:ring_strategy].()
    end

end