Sha256: 60f7594e115b5d06fb878c7d0c8f73ba34d440000eb58eecab0ebd66fe3e7e82

Contents?: true

Size: 608 Bytes

Versions: 30

Compression:

Stored size: 608 Bytes

Contents

class Array
  # returns the first element of a 2-element array.
  # useful when dealing with hashes in array form.
  # e.g., pairs.map(&:key)
  def key
    size == 2 or raise 'array must contain exactly two elements'
    first
  end

  # returns the second element of a 2-element array.
  # useful when dealing with hashes in array form.
  # e.g., pairs.map(&:value)
  def value
    size == 2 or raise 'array must contain exactly two elements'
    last
  end

  # A backport of Array@to_h from Ruby 2.1
  unless instance_methods.include?(:to_h)
    define_method(:to_h) do
      Hash[self]
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
abstractivator-0.20.0 lib/abstractivator/array_ext.rb
abstractivator-0.19.0 lib/abstractivator/array_ext.rb
abstractivator-0.18.0 lib/abstractivator/array_ext.rb
abstractivator-0.17.0 lib/abstractivator/array_ext.rb
abstractivator-0.16.0 lib/abstractivator/array_ext.rb
abstractivator-0.15.0 lib/abstractivator/array_ext.rb
abstractivator-0.14.0 lib/abstractivator/array_ext.rb
abstractivator-0.13.0 lib/abstractivator/array_ext.rb
abstractivator-0.12.0 lib/abstractivator/array_ext.rb
abstractivator-0.11.0 lib/abstractivator/array_ext.rb
abstractivator-0.10.0 lib/abstractivator/array_ext.rb
abstractivator-0.8.0 lib/abstractivator/array_ext.rb
abstractivator-0.7.0 lib/abstractivator/array_ext.rb
abstractivator-0.6.0 lib/abstractivator/array_ext.rb
abstractivator-0.5.0 lib/abstractivator/array_ext.rb
abstractivator-0.4.1 lib/abstractivator/array_ext.rb
abstractivator-0.4.0 lib/abstractivator/array_ext.rb
abstractivator-0.3.1 lib/abstractivator/array_ext.rb
abstractivator-0.3.0 lib/abstractivator/array_ext.rb
abstractivator-0.2.0 lib/abstractivator/array_ext.rb