Daniel Geisler
Daniel Geisler

Reputation: 123

Ruby inheriting a module class not working

I'm trying to write a class "web" in Ruby 2.0.0 that inherits from GEXF::Graph, but I am unable to get the Graph methods like Web.define_node_attribute to work. I'm a new ruby programmer, so I expect I'm doing something goofy. Thanks.

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  

Upvotes: 1

Views: 62

Answers (1)

snowe
snowe

Reputation: 1375

The issue is that you are monkey-patching the GEXF::Graph initialize method without calling super on it. What you did was essentially 'write-over' the initialize method that needed to be called. To fix this, change your initialize method to call the super method first:

  def initialize
    super
    @pages = Array.new
  end  

Upvotes: 1

Related Questions