Sha256: c0069f2f55fdc77d6badcb786aebaa7993b2fd4a3fbc52706fff02863d470bd5

Contents?: true

Size: 1.03 KB

Versions: 7

Compression:

Stored size: 1.03 KB

Contents

class Hash

  # Merge the values of this hash with those from another, setting all values
  # to be arrays representing the values from both hashes.
  #
  #   { :a=>1, :b=>2 }.collate :a=>3, :b=>4, :c=>5
  #   #=> { :a=>[1,3], :b=>[2,4], :c=>[5] }
  #
  #  CREDIT: Gavin Kistner (Phrogz)

  def collate(other_hash)
    dup.collate!(other_hash)
  end

  # The same as #collate, but modifies the receiver in place.

  def collate!(other_hash)
    # Prepare, ensuring every existing key is already an Array
    each do |key, value|
      if value.is_a?(Array)
        self[key] = value
      else
        self[key] = [value]
      end
    end
    # Collate with values from other_hash
    other_hash.each do |key, value|
      if self[key]
        if value.is_a?(Array)
          self[key].concat( value )
        else
          self[key] << value
        end
      elsif value.is_a?(Array)
        self[key] = value
      else
        self[key] = [value]
      end
    end
    #each{ |key, value| value.uniq! } if options[ :uniq ]
    self
  end

end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
facets-2.4.0 lib/facets/hash/collate.rb
facets-2.4.1 lib/facets/hash/collate.rb
facets-2.4.2 lib/core/facets/hash/collate.rb
facets-2.4.3 lib/core/facets/hash/collate.rb
facets-2.4.4 lib/core/facets/hash/collate.rb
facets-2.4.5 lib/core/facets/hash/collate.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/hash/collate.rb