Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby - override method and then revert

I am trying to find a way that I can override a method, do something, and then revert without leaving any artifacts around.

I have implemented this using mocha but obviously this is not going to fly in a production app. Notice the new method has parameters and the old one does not.

Example as follows

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
like image 511
stellard Avatar asked Apr 20 '11 17:04

stellard


2 Answers

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
like image 195
steenslag Avatar answered Nov 16 '22 12:11

steenslag


Another way to do that, without creating an extra method, is this:

class Foo
  def bar
    :old_method
  end
end

Foo.new.bar # => :old_method

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

class Foo
  def bar
    :new_method
  end
end

Foo.new.bar # => :new_method

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

Foo.new.bar # => :old_method

I think that this is better than using an alias method. In Ruby methods are, also, objects. I just take the reference of the object before destructing the association of the object (the method) with the class. After I add the same method. It also works if you use the undef keyword to remove the method from the class. The bad point is that you have to have an object of the class to take the reference of the method.

like image 43
Guilherme Bernal Avatar answered Nov 16 '22 11:11

Guilherme Bernal