Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want it another way, take a look at "strategy" design patten: </p> <pre><code># test_mach.rb require 'rubygems' require 'mechanize' # this is the context class,which calls the different strategy implementation class Crawler def initialize(some_website_strategy) @strategy = some_website_strategy end def crawl begin @strategy.crawl #execute code in Amazon class here? rescue Exception =&gt; e puts "==== starts this exception comes from Parent Class" puts e.backtrace puts "==== ends this exception comes from Parent Class" end end end # strategy class for Amazon class Amazon def crawl puts "now crawling amazon" raise "oh ... some errors when crawling amazon" end end # strategy class for taobao.com class Taobao def crawl puts "now crawling taobao" raise "oh ... some errors when crawling taobao" end end </code></pre> <p>then run this code:</p> <pre><code>amazon = Crawler.new(Amazon.new) amazon.crawl taobao = Crawler.new(Taobao.new) taobao.crawl </code></pre> <p>result: </p> <pre><code>now crawling amazon ==== starts this exception comes from Parent Class test_mach.rb:27:in `crawl' test_mach.rb:13:in `crawl' test_mach.rb:38 ==== ends this exception comes from Parent Class now crawling taobao ==== starts this exception comes from Parent Class test_mach.rb:34:in `crawl' test_mach.rb:13:in `crawl' test_mach.rb:40 ==== ends this exception comes from Parent Class </code></pre> <p>BTW. for your implementation, basically I did the same way with you. except </p> <pre><code># my implementation class Crawler def stuff raise "abstract method called" end end </code></pre> <p>If you want it another way, take a look at "around alias" (&lt;&lt; metaprogramming ruby>>, page 155 ) . However I think "around alias" is the revert case from strategy. </p> <p>( I mean, </p> <ul> <li>in strategy pattern, you at first implement the "context" class, then you need to implement the "strategy" class. </li> <li>but in "around alias" spell, you need to first of all get an "strategy" implemented, then you write a new class that "around alias" the "strategy"... </li> </ul> <p>err... hope I didn't make you confused ^_^ )</p>
 

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