我有以下方法太长了:
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 将代码行数减少一半?