timocratic (owner)

Revisions

gist: 222710 Download_button fork
public
Description:
An example of differing ways of abstracting initialization into the parent
Public Clone URL: git://gist.github.com/222710.git
Embed All Files: show embed
a_ too_much_in_the_parent_the_wrong_way.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class AbstractScraper
  def initialize(username, password)
    @agent = WWW::Mechanize.new
    @agent.user_agent_alias = "Windows Mozilla"
    @agent.keep_alive = false
    login(username, password)
  end
end
 
class NormalAScraper < AbstractScraper
  
end
 
class NormalBScraper < AbstractScraper
  
end
 
class GoofyScraper < AbstractScraper
  def initialize
    @agent = WWW::Mechanize.new
    @agent.user_agent_alias = "Windows Mozilla"
    @agent.keep_alive = false
    login
  end
end
 
b_better_composition.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class AbstractScraper
  def initialize(username, password)
    initialize_agent
    login(username, password)
  end
 
  def initialize_agent
    @agent = WWW::Mechanize.new
    @agent.user_agent_alias = "Windows Mozilla"
    @agent.keep_alive = false
  end
end
 
class NormalAScraper < AbstractScraper
  
end
 
class NormalBScraper < AbstractScraper
  
end
 
class GoofyScraper < AbstractScraper
  def initialize
    initialize_agent
    login
  end
end
 
c_that_and_each_class_init_is_more_obvious_imo.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class AbstractScraper
  def initialize_agent
    @agent = WWW::Mechanize.new
    @agent.user_agent_alias = "Windows Mozilla"
    @agent.keep_alive = false
  end
end
 
class NormalAScraper < AbstractScraper
  def initialize(username, password)
    initialize_agent
    login(username, password)
  end
end
 
class NormalBScraper < AbstractScraper
    def initialize(username, password)
    initialize_agent
    login(username, password)
  end
end
 
class GoofyScraper < AbstractScraper
  def initialize
    initialize_agent
    login
  end
end