2014-01-27 72 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

井你共享你沒有一個'Web'類中定義做的代碼,所以這是第一個問題,除非你有它定義沒有包含在你的代碼中。 –

+0

對不起,是的,我已經定義了一個Web類,並嘗試了我所知的所有方法來使其工作。 –

+0

顯示代碼,我沒有看到問題 –

回答

1

的問題是,你是猴子修補GEXF::Graph initialize方法不就可以調用超。你所做的基本上是'寫入'需要調用的初始化方法。爲了解決這個問題,改變你的初始化方法調用超級方法第一:

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

謝謝!代碼現在正在工作。 –