Module: ROXML::CoreExtensions::Array::Conversions

Included in:
Array
Defined in:
lib/roxml/extensions/array/conversions.rb

Instance Method Summary collapse

Instance Method Details

#apply_to(val) ⇒ Object



28
29
30
31
# File 'lib/roxml/extensions/array/conversions.rb', line 28

def apply_to(val)
  # Only makes sense for arrays of blocks... maybe better outside Array...
  inject(val) {|val, block| block.call(val) }
end

#to_hObject

:nodoc:



23
24
25
# File 'lib/roxml/extensions/array/conversions.rb', line 23

def to_h #:nodoc:
  to_hash
end

#to_hashObject

Translates an array into a hash, where each element of the array is an array with 2 elements:

>> [[:key, :value], [1, 2], ['key', 'value']].to_h
=> {:key => :value, 1 => 2, 'key' => 'value'}


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/roxml/extensions/array/conversions.rb', line 11

def to_hash
  hash = inject({}) do |result, (k, v)|
    result[k] ||= []
    result[k] << v
    result
  end
  hash.each_pair do |k, v|
    hash[k] = v.first if v.one?
  end
  hash
end