Method: Hash#dearray_values

Defined in:
lib/core/facets/hash/dearray_values.rb

#dearray_values(index = 0) ⇒ Object

Any array values will be replaced with the first element of the array. Arrays with no elements will be set to nil.

h = { :a=>[1], :b=>[1,2], :c=>3, :d=>[] }
h.dearray_values  #=> { :a=>1, :b=>1, :c=>3, :d=>nil }

CREDIT: Trans



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/core/facets/hash/dearray_values.rb', line 11

def dearray_values(index=0)
  h = {}
  each do |k,v|
    case v
    when Array
      h[k] = v[index] || v[-1]
    else
      h[k] = v
    end
  end
  h
end