Method: Array#to_assoc_hash
- Defined in:
- lib/jinx/helpers/array.rb
#to_assoc_hash ⇒ Hash
Returns a new Hash generated from this array of arrays by associating the first element of each member to the remaining elements. If there are only two elements in the member, then the first element is associated with the second element. If there is less than two elements in the member, the first element is associated with nil. An empty array is ignored.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/jinx/helpers/array.rb', line 64 def to_assoc_hash hash = {} each do |item| raise ArgumentError.new("Array member must be an array: #{item.pp_s(:single_line)}") unless Array === item key = item.first if item.size < 2 then value = nil elsif item.size == 2 then value = item[1] else value = item[1..-1] end hash[key] = value unless key.nil? end hash end |