書籍「リファクタリングruby」8.6 ハッシュからオブジェクトへ。異なる種類のオブジェクトを格納し、復数の目的のために渡され使用されるhashはhashのキーに何が格納されているか分からなくなる。ということでオブジェクトする。

 1 class NetWork
 2   attr_reader :name
 3 
 4   def initialize(name)
 5     @name = name
 6   end
 7 end
 8 
 9 class Node
10   attr_reader :new_network
11   def initialize(network)
12     @new_network = network
13   end
14 end
15 
16 a_net, b_net, c_net  = NetWork.new("a_net"), NetWork.new("b_net"), NetWork.new("c_net")
17 a_node, b_node, c_node = Node.new(a_net), Node.new(b_net), Node.new(c_net)
18 
19 new_network = { nodes: [], old_networks: [] }
20 new_network[:old_networks] << a_node.new_network
21 new_network[:nodes] << a_node
22 
23 new_network[:old_networks] << b_node.new_network
24 new_network[:nodes] << b_node
25 
26 new_network[:old_networks] << c_node.new_network
27 new_network[:nodes] << c_node
28 
29 new_network[:name] = new_network[:old_networks].collect {|net_work| net_work.name}.join(" - ")

new_networkは異なる種類のオブジェクトが格納されている

 1 class ResultNetwork
 2   attr_accessor :old_networks, :nodes
 3 
 4   def initialize
 5     @nodes, @old_networks = [], []
 6   end
 7 
 8   def [](attribute)
 9     instance_variable_get("@#{attribute}")
10   end
11 
12   def []=(attribute, value)
13     instance_variable_set("@#{attribute}", value)
14   end
15 end
16 
17 new_network = { nodes: [], old_networks: [] }
18 new_network[:old_networks] << a_node.new_network
19 new_network[:nodes] << a_node
20 
21 new_network[:old_networks] << b_node.new_network
22 new_network[:nodes] << b_node
23 
24 new_network[:old_networks] << c_node.new_network
25 new_network[:nodes] << c_node
26 
27 new_network[:name] = new_network[:old_networks].collect {|net_work| net_work.name}.join(" - ")
28 
29 new_network = ResultNetwork.new

ひとまずinstance_variable_get instance_variable_setを使用しhashをオブジェクトに置き換えても既存処理が通るようにする。

 1 class ResultNetwork
 2   attr_accessor :old_networks, :nodes, name
 3 
 4   def initialize
 5     @nodes, @old_networks = [], []
 6   end
 7 end
 8 
 9 new_network.old_networks << a_node.new_network
10 new_network.nodes << a_node
11 
12 new_network.old_networks << b_node.new_network
13 new_network.nodes << b_node
14 
15 new_network.old_networks << c_node.new_network
16 new_network.nodes << c_node
17 
18 new_network.name = new_network.old_networks.collect {|net_work| net_work.name}.join(" - ")

置き換える

 1 class ResultNetwork
 2   attr_accessor :old_networks, :nodes
 3 
 4   def initialize
 5     @nodes, @old_networks = [], []
 6   end
 7 
 8   def [](attribute)
 9     instance_variable_get("@#{attribute}")
10   end
11 
12   def []=(attribute, value)
13     instance_variable_set("@#{attribute}", value)
14   end
15 
16   def name
17     old_networks.collect {|network| network.name}.join(" - ")
18   end
19 end
20 
21   new_network = ResultNetwork.new
22 
23   new_network.old_networks << a_node.new_network
24   new_network.nodes << a_node
25 
26   new_network.old_networks << b_node.new_network
27   new_network.nodes << b_node
28 
29   new_network.old_networks << c_node.new_network
30   new_network.nodes << c_node
31 
32   new_network.name

nameメソッドを移すことができ、いっそうすっきり




comments powered by Disqus


© 2015 kyuden