0

New to ruby. Wrote a program now trying metaprogramming. As you can see i have the code below. Have marked common statements. How do I write these in one place and use them again and again. The statements are part of a loop. So its like I just want to have the ability to insert the statements. Tried a proc it seems to work. But have not understood it really well. The jargon gets to me. What should i read up on really well and what is a good source. there are 3 loops to do. the loops starting is different based on condition adn only one statement is different in each loop. how do i write this cleaning and DRY

@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

1 Answers1

2

What you asking is, i.e. block and yield. It works like this:

def add_with_extra(a, b)
  c = a + b
  d = yield(c)
  c + d
end

# > add_with_extra(3, 5) { |c| c * 2 }
# => 24
# > add_with_extra(3, 5) { |c| c / 2 }
# => 12

But in your case it will look like this:

case key
  when :morals
    ids.each_with_index do |id, index|
      do_processing(ids, basic_relation, key_ids, key, id, index) do |relation, ids, index, id|
        relation[:values] = S(@ids[:values][index])
      end
    end
  when :values
    ids.flatten.uniq.each do |id|
      do_processing(ids, basic_relation, key_ids, key, id, index) do |relation, ids, index, id|
        ids.each_with_index { |array, index| relation[:morals] = S(A(relation[:morals]) - A(@ids[:morals][index])) unless array.include? id }
      end
    end
  else
    ids.each do |id|
      do_processing(ids, basic_relation, key_ids, key, id, index)
    end
end

Which is not really well readable and understandable. Instead I suggest to make some refactoring:

def prepare_relation(basic_relation, key, id)
  relation = basic_relation.dup
  relation[:for] = key
  relation[key] = id
  relation
end

def add_to_stack(relation, key_ids, key, id)
  @stack << relation
  key_ids << id unless @stack.find{ |relation| relation.class != Hash && relation[:for] == key.to_s && relation[key] == id }
end

basic_relation = {for: nil, state: nil}
@objects.each_key { |key| basic_relation[key] = nil unless key == :flows }
basic_relation.merge!(ids_string)

@ids.each do |key, ids|
  next if key == :flows
  key_ids = []
  lookup_ids = key == :values ? ids.flatten.uniq : ids

  lookup_ids.each_with_index do |id, index|
    relation = prepare_relation(basic_relation, key, id)
    relation[:values] = S(@ids[:values][index]) if key == :morals
    if key == :values
      ids.each_with_index do |array, index|
        relation[:morals] = S(A(relation[:morals]) - A(@ids[:morals][index])) unless array.include? id
      end
    end
    add_to_stack(relation, key_ids, key, id)
  end

  unless key_ids.empty?
    TaleRelation.where(for: key , key: key_ids).each do |activerecord|
      activerecord[:state] = nil
      @stack << activerecord
    end
  end
end

Here i generalized the main difference of your switch:

when :morals
    ids.each_with_index do |id, index|
...
when :values
    ids.flatten.uniq.each do |id|
...
else
    ids.each do |id|

The real difference is only with :values case, because each_with_index is suitable for the last case also - we just won't be using index. Then what was not common turns into simple two if's:

relation[:values] = S(@ids[:values][index]) if key == :morals
if key == :values
  ids.each_with_index do |array, index|
    relation[:morals] = S(A(relation[:morals]) - A(@ids[:morals][index])) unless array.include? id
  end
end

P.S. You shouldn't call methods A or S. Methods names must be lowercased and should have a meaning.

Yuri Golobokov
  • 1,829
  • 12
  • 11