2014-01-27 2 views
1

GEXF :: Graph를 상속 한 Ruby 2.0.0에서 "web"클래스를 작성하려고하는데 Web.define_node_attribute와 같은 Graph 메소드를 사용할 수 없습니다. 저는 새로운 루비 프로그래머입니다. 그래서 나는 바보 같은 짓을하고 있다고 생각합니다. 감사.모듈 클래스를 상속 한 Ruby가 작동하지 않습니다.

webrun.rb

require 'rubygems' 
require 'gexf' 
require 'anemone' 
require 'mechanize' 
require_relative 'web' 

web = Web.new 
web.define_node_attribute(:url) 
web.define_node_attribute(:links,  
          :type => GEXF::Attribute::BOOLEAN, 
          :default => true) 

web.rb

require 'rubygems' 
require 'gexf' 
require 'anemone' 
require 'mechanize' 

class Web < GEXF::Graph 

    attr_accessor :root 
    attr_accessor :pages 

    def initialize 
    @pages = Array.new 
    end 

    def pages 
    @pages 
    end 

    def add page 
    @pages << page 
    end 

    def parse uri, protocol = 'http:', domain = 'localhost', file = 'index.html' 
    u = uri.split('/') 
    if n = /^(https?:)/.match(u[0]) 
     protocol = n[0] 
     u.shift() 
    end 
    if u[0] == '' 
     u.shift() 
    end 
    if n = /([\w\.]+\.(org|com|net))/.match(u[0]) 
     domain = n[0] 
     u.shift() 
    end 
    if n = /(.*\.(html?|gif))/.match(u[-1]) 
     file = n[0] 
     u.pop() 
    end 
    cnt = 0 
    while u[cnt] == '..' do 
     cnt = cnt + 1 
     u.shift() 
    end 
    while cnt > 0 do 
     cnt = cnt - 1 
     u.shift() 
    end 
    directory = '/'+u.join('/') 
    puts "protocol: " + protocol + " domain: " + domain + \ 
     " directory: " + directory + " file: " + file 
    protocol + "//" + domain + directory + (directory[-1] == '/' ? '/' : '') + file  
    end 

    def crawl 
    Anemone.crawl(@root) do |anemone| 
     anemone.on_every_page do |sitepage| 
     add sitepage 
     end 
    end 
    end  

    def save file  
    f = File.open(file, mode = "w") 
    f.write(to_xml) 
    f.close() 
    end 

end 
+0

공유 된 코드에는 정의 된 '웹'클래스가 없으므로 정의되지 않은 첫 번째 문제가됩니다. 코드에 포함시키지 않았습니다. –

+0

죄송합니다, 예 웹 클래스를 정의하고 작동하도록 모든 방법을 시도했습니다. –

+0

코드를 보여줍니다. 문제가 보이지 않습니다. –

답변

1

문제는 원숭이 패치 거기에 슈퍼를 호출하지 않고 GEXF::Graph 초기화 방법이라는 것이다. 당신이 한 것은 필연적으로 호출되어야하는 initialize 메소드의 'write-over'였습니다. 이 문제를 해결하려면 먼저 슈퍼 메서드를 호출하도록 초기화 메소드를 변경하십시오.

def initialize 
    super 
    @pages = Array.new 
    end 
+1

고마워요! 코드가 작동 중입니다. –

관련 문제