Sha256: 7c0dfc0a6c4396372b898444b97183501818c8e17ba77e237a24cb72329ac6e9
Contents?: true
Size: 877 Bytes
Versions: 15
Compression:
Stored size: 877 Bytes
Contents
class Hash # Any array values with 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 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 # Any array values with one or no elements will be set to the element # or nil. # # h = { :a=>[1], :b=>[1,2], :c=>3, :d=>[] } # h.dearray_singular_values #=> { :a=>1, :b=>[1,2], :c=>3, :d=>nil } # # CREDIT: Trans def dearray_singular_values h = {} each do |k,v| case v when Array h[k] = (v.size < 2) ? v[0] : v else h[k] = v end end h end end
Version data entries
15 entries across 14 versions & 1 rubygems