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

谢谢!代码现在正在工作。 –