0
紅寶石的新成員。現在寫一個程序嘗試元編程。正如你可以看到我有下面的代碼。有明顯的共同聲明。我如何在一個地方寫這些並一次又一次地使用它們。 這些語句是循環的一部分。所以它就像我只想有能力插入語句。試過一個proc它似乎工作。但還沒有真正理解它。行話來到我身上。 我應該怎樣讀得很好,什麼是良好的來源。 有3個循環。循環開始是不同的條件和條件只有一個語句是不同的每個循環。我如何寫這個清潔和乾燥可重複使用的代碼塊 - Ruby塊,Procs,Lambdas
@ids.each do |key, ids|
key_ids = []
case key
when :flows then next
when :morals
ids.each_with_index do |id, index|
relation = {for: nil, state: nil}; @objects.each_key { |key| relation[key] = nil unless key == :flows } #Common A
relation.merge!(ids_string); relation[:for] = key; relation[key] = id #Common B
relation[:values] = S(@ids[:values][index])
@stack << relation #Common C
key_ids << id unless @stack.find{ |relation| relation.class != Hash && relation[:for] == key.to_s && relation[key] == id } #Common D
end
when :values
ids.flatten.uniq.each do |id|
relation = {for: nil, state: nil}; @objects.each_key { |key| relation[key] = nil unless key == :flows } #Common A
relation.merge!(ids_string); relation[:for] = key; relation[key] = id; #Common B
ids.each_with_index { |array, index| !array.include?(id) ? relation[:morals] = S(A(relation[:morals]) - A(@ids[:morals][index])) :() }
@stack << relation #Common C
key_ids << id unless @stack.find{ |relation| relation.class != Hash && relation[:for] == key.to_s && relation[key] == id } #Common D
end
else
ids.each do |id|
relation = {for: nil, state: nil}; @objects.each_key { |key| relation[key] = nil unless key == :flows } #Common A
relation.merge!(ids_string); relation[:for] = key; relation[key] = id #Common B
@stack << relation #Common C
key_ids << id unless @stack.find{ |relation| relation.class != Hash && relation[:for] == key.to_s && relation[key] == id } #Common D
end
end
!key_ids.empty? ? TaleRelation.where(for: key , key => key_ids).each { |activerecord| activerecord[:state] = nil; @stack << activerecord } :()
end