1

Mechanize :: Cookie 오작동과 관련된 문제가 있으며 원숭이 패치를하려고합니다. 내 코드 :class << self, alias_method 및 monkey patching Mechanize :: Cookie

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 

나는이를 추가 할 때 쿠키가 추가되지 않습니다 나는 이유를 알아낼 수 없습니다.

편집 : 이 문제와 원숭이 패치없이이 코드를 시도 보려면 : 패치없이

agent = Mechanize.new 
agent.get 'http://www.google.com/' 
pp agent.cookie_jar 

당신이 빈 하나 그것으로, 전체 쿠키 항아리를 볼 수 있습니다.

+0

합니까 더 명확하게하려면 인쇄 해 줄까? –

+0

예, "새로운 구문 분석!" 나는 체크 아웃하기 위해 'puts'라인을 거기에 놓았 기 때문에 old_parse가 호출되는 것을 알 수 있습니다. 그러나이 코드를 포함 시키면 쿠키가 저장되지 않습니다. – pguardiario

+0

코드를 포함하지 않으면 쿠키가 저장되었습니다. 맞습니까? –

답변

4

원본 구문 분석 방법에 yield cookie if block_given? 문이있는 것 같습니다. 블록을 전달할 수 있어야합니다.

편집 : "새로운 해석"

이 ...

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}'"} 
+0

감사합니다. z5h입니다. 이것이 올바른 대답이라고 생각합니다. 네가 가진다면 또 다른 힌트를 쓸 수있다. – pguardiario

+0

그게 전부입니다. 감사! – pguardiario

관련 문제