ruby - 覆盖方法然后还原

时间:2011-04-20 17:54:14

标签: ruby extension-methods

我正试图找到一种方法,我可以覆盖一个方法,做一些事情,然后恢复而不留下任何文物。

我已经使用mocha实现了这一点,但显然这不会在生产应用程序中飞行。请注意,新方法有参数,而旧方法没有参数。

示例如下

require 'rubygems'
require 'mocha'

class Example

  def to_something
    self.stubs(:attribs => other(1))
    r = attribs_caller
    self.unstub(:attribs)
    r
  end

  def other(int)
    {"other" => int }
  end

  def attribs_caller
    attribs
  end

  def attribs
    {"this" => 1 }
  end

end

a1 = Example.new

puts a1.attribs_caller  #=> this1
puts a1.to_something    #=> other1
puts a1.attribs_caller  #=> this1

2 个答案:

答案 0 :(得分:8)

class String
  alias orig_reverse reverse
  def reverse(n)
    'fooled you. '*n
  end
end

puts "ab".reverse(2)
#=> fooled you fooled you

# clean up:
class String
  alias reverse orig_reverse
  remove_method(:orig_reverse)
end

puts "ab".reverse #=> ba

答案 1 :(得分:3)

另一种方法是在不创建额外方法的情况下:

class Foo
  def bar
    :old_method
  end
end

Foo.new.foo # => :old_method

$old_method = Foo.new.method(:bar)

class Foo
  def bar
    :new_method
  end
end

Foo.new.foo # => :new_method

class Foo
  define_method($old_method.name, &$old_method)
end

Foo.new.foo # => :old_method

我认为这比使用别名方法更好。在Ruby中,方法也是对象。我只是在破坏对象(方法)与类的关联之前获取对象的引用。我添加相同的方法后。如果您使用undef关键字从类中删除方法,它也可以工作。不好的一点是你必须有一个类的对象来接受方法的引用。

相关问题