Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby execute code in class getting inherited to
    primarykey
    data
    text
    <p>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. </p> <p>Here are the two classes I have:</p> <pre><code>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 =&gt; 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 &lt; 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 </code></pre> <p>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?</p> <p>EDIT: This is when I ended up doing</p> <pre><code>require 'mechanize' class Crawler Mechanize.html_parser = Nokogiri::HTML def initialize @agent = Mechanize.new end def crawl yield rescue Exception =&gt; e puts "Exception: #{e.message}" puts "On url: #{@current_url}" puts e.backtrace end def get(url) @current_url = url @agent.get(url) end end c = Crawler.new c.crawl do page = c.get("http://www.amazon.com") puts page.parser.xpath("//asldkfjasdlkj").first['href'] end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload