Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/ext/array.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#to_h(arrayed = nil) ⇒ Object

Extraceted from Ruby Facets: Converts a two-element associative array into a hash.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ext/array.rb', line 5

def to_h(arrayed=nil)
  h = {}
  if arrayed
    each{ |k,*v| h[k] = v }
  else
    ary = []
    each do |a|
      Array===a ? ary.concat(a) : ary << a
    end
    h = Hash[*ary]
  end
  h
end