Sha256: cec4c7c6ca4eddf5b85ecf2dd7defaab11e026fa2c27ddbb3c6eaf3ddf5474c8

Contents?: true

Size: 916 Bytes

Versions: 11

Compression:

Stored size: 916 Bytes

Contents

module ROXML
  module CoreExtensions
    module Array #:nodoc:all
      module Conversions
        # Translates an array into a hash, where each element of the array is
        # an array with 2 elements:
        #
        #   >> [[:key, :value], [1, 2], ['key', 'value']].to_h
        #   => {:key => :value, 1 => 2, 'key' => 'value'}
        #
        def to_hash
          hash = inject({}) do |result, (k, v)|
            result[k] ||= []
            result[k] << v
            result
          end
          hash.each_pair do |k, v|
            hash[k] = v.first if v.one?
          end
          hash
        end

        def to_h #:nodoc:
          to_hash
        end
        deprecate :to_h => :to_hash

        def apply_to(val)
          # Only makes sense for arrays of blocks... maybe better outside Array...
          inject(val) {|val, block| block.call(val) }
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
Empact-roxml-2.4.1 lib/roxml/extensions/array/conversions.rb
Empact-roxml-2.4.2 lib/roxml/extensions/array/conversions.rb
Empact-roxml-2.4.3 lib/roxml/extensions/array/conversions.rb
Empact-roxml-2.5.1 lib/roxml/extensions/array/conversions.rb
roxml-2.4.1 lib/roxml/extensions/array/conversions.rb
roxml-2.4.3 lib/roxml/extensions/array/conversions.rb
roxml-2.5.1 lib/roxml/extensions/array/conversions.rb
roxml-2.4.2 lib/roxml/extensions/array/conversions.rb
roxml-2.5.0 lib/roxml/extensions/array/conversions.rb
roxml-2.5.3 lib/roxml/extensions/array/conversions.rb
roxml-2.5.2 lib/roxml/extensions/array/conversions.rb