I have an issue with Mechanize::Cookie misbehaving and I want to trying to monkey patch it. My code:
class Mechanize::Cookie
class << self; alias_method :old_parse, :parse end
def self.parse(uri, str, log = Mechanize.log)
puts 'new parse!'
#str.gsub!(/domain[^;]*;/,'')
old_parse(uri, str, log)
end
end
when I add this, the cookies don't get added and I can't figure out why.
Edit: To see the problem try this code with and without the monkey patch:
agent = Mechanize.new
agent.get 'http://www.google.com/'
pp agent.cookie_jar
Without patch you will see a full cookie jar, with it an empty one.
Looks like the original parse method has a yield cookie if block_given?
statement in it. You'll need to be able to pass a block as well.
EDIT:
To be more clear...
class Foo
def self.x
yield "yielded from x!" if block_given?
end
end
class Foo
class <<self
alias :y :x
end
# new implementation of x's last parameter is an optional block
def self.x(&block)
puts "in redefined x."
puts "block=#{block}"
self.y(&block) #use the block as the last parameter
end
end
Foo.x{|value| puts "value is '#{value}'"}