Module: Enumerable

Defined in:
lib/mug/bool.rb,
lib/mug/to_h.rb

Instance Method Summary collapse

Instance Method Details

#to_bObject

Converts enum to a boolean. Returns true if there are any elements.



86
87
88
# File 'lib/mug/bool.rb', line 86

def to_b
  any?{ true }
end

#to_hObject

Converts enum to a Hash.

Each element of enum must be a single item, or an array of two items. Duplicate keys are overwritten in order.

[].to_h             #=> {}
[1,2].to_h          #=> {1=>nil, 2=>nil}
(1..2).to_h         #=> {1=>nil, 2=>nil}
[[1,2],[3,4]].to_h  #=> {1=>2, 3=>4}
[[1,2],[1,4]].to_h  #=> {1=>4}


23
24
25
26
27
28
29
30
# File 'lib/mug/to_h.rb', line 23

def to_h
  hsh = {}
  each do |k,v,*x|
    raise ArgumentError, "invalid number of elements (#{x.length+2} for 1..2)" if x.any?
    hsh[k] = v
  end
  hsh
end