Merge two arrays of hashes and map them I would like to refactor that code.

def experiences
  work_experience_array = @coach.delete(:work_experience)
  education_array = @coach.delete(:education)

  array = []
  array &lt;&lt; work_experience_array.map do |experience|
    Db::Experiences::Work.new(experience.for_indexing)
  end
  array &lt;&lt; education_array.map do |experience|
    Db::Experiences::Education.new(experience.for_indexing)
  end
  array.flatten
end
Some notes:


If you use += instead of &lt;&lt;, you don't need to flatten the array.
DRY code by identifying common patterns and create suitable data structures accordingly.


A rewrite capturing the pattern:

def experiences
  fields = [
    [:work_experience, Db::Experiences::Work], 
    [:education, Db::Experiences::Education],
  ]

  fields.flat_map do |field, db_class|
    @coach.delete(field).map do |experience|
      db_class.new(experience.for_indexing)
    end
  end
end