1
我有以下的方法實在太長了:傳遞一個對象作爲參數
def combine_atoms
@left.each do |chemical|
chemical.chem_species.each do |atom|
if @left_total[atom[0]].nil?
@left_total[atom[0]] = atom[1]
else
@left_total[atom[0]] += atom[1]
end
end
end
@right.each do |chemical|
chemical.chem_species.each do |atom|
if @right_total[atom[0]].nil?
@right_total[atom[0]] = atom[1]
else
@right_total[atom[0]] += atom[1]
end
end
end
end
一個怎麼能忽略@left
和@left_total
作爲參數,使用Ruby減少一半的代碼行數?