2012-10-12 2 views
1

libxml의 SAX 파서 (illustrated here)를 사용하려고하지만 정의되지 않은 메서드 오류가 발생합니다.libxml의 색소폰 파서를 사용할 때 정의되지 않은 메서드 오류

내가 그것을 실행하면 내 코드와

$domain_topics = Hash.new { |h,d| h[d] = [] } 
parser = LibXML::XML::SaxParser.io(
    File.open("content.rdf.u8", "r:UTF-8") 
) 
class Callbacks 
    include LibXML::XML::SaxParser::Callbacks 
    def initialize 
    @state = :top 
    @topics = nil 
    end 
    def on_start_element(element, attributes) 
    case @state 
    when :top 
     return unless element == 'ExternalPage' 
     @state = :ExternalPage 

     domain = attributes['about'].sub(%r!^\w+://([^"/]*)(?:/[^"]*)?$!, '\1') 
     @topics = $domain_topics[domain] 
    when :ExternalPage 
     return unless element == 'topic' 
     @state = :topic 
    end 
    end 
    def on_characters(characters) 
    if @state == :topic and @topics 
     @topics << characters 
    end 
    end 
    def on_end_element(element) 
    case @state 
    when :ExternalPage 
     @state = :top 
     @topics = nil 
    when :topic 
     @state = :ExternalPage 
    end 
    end 
end 
parser.callbacks = Callbacks 
parser.parse 

입니다 : 내가 잘못 여기서 뭐하는 거지

% ./my_awesome_code.rb 
./my_awesome_code.rb:1337:in `parse': undefined method `on_start_document' for Callbacks:Class (NoMethodError) 

? include LibXML::XML::SaxParser::Callbackson_start_document에 대한 기본 정의를 지정하면 안됩니까?

IRB 내 직감 확인하는 것 같다

1.9.3p194 :009 > Callbacks.instance_methods.include? :on_start_document 
=> true 

답변

0

내가 바보 같은 생각을하고 #callbacks= 적절한 콜백이 아닌 클래스와 함께 예를을 통과 할 필요가 있음을 놓쳤다. 그래서 내가해야 할 일은 할일이었다.

parser.callbacks = Callbacks.new