Ruby execute code in class getting inherited to
        Posted  
        
            by 
                AdamB
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by AdamB
        
        
        
        Published on 2012-04-06T22:25:08Z
        Indexed on 
            2012/04/06
            23:30 UTC
        
        
        Read the original article
        Hit count: 286
        
I'm trying to be able to have a global exception capture where I can add extra information when an error happens. I have two classes, "crawler" and "amazon". What I want to do is be able to call "crawl", execute a function in amazon, and use the exception handling in the crawl function.
Here are the two classes I have:
require 'mechanize'
class Crawler
  Mechanize.html_parser = Nokogiri::HTML
  def initialize
    @agent = Mechanize.new
  end
  def crawl
    puts "crawling"
    begin
      #execute code in Amazon class here?
    rescue Exception => e
      puts "Exception: #{e.message}"
      puts "On url: #{@current_url}"
      puts e.backtrace
    end
  end
  def get(url)
    @current_url = url
    @agent.get(url)
  end
end
class Amazon < Crawler
  #some code with errors
  def stuff
    page = get("http://www.amazon.com")
    puts page.parser.xpath("//asldkfjasdlkj").first['href']
  end
end
a = Amazon.new
a.crawl
Is there a way I can call "stuff" inside of "crawl" so I can use that exception handling over the entire stuff function? Is there a better way to accomplish this?
© Stack Overflow or respective owner