Module: Monkey::Ext::Enumerable

Defined in:
lib/monkey/ext/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#construct(obj) ⇒ Object



4
5
6
7
# File 'lib/monkey/ext/enumerable.rb', line 4

def construct(obj)
  enum_for :construct, obj unless block_given? or !respond_to? :enum_for
  inject(obj) { |a,v| a.tap { yield(a, v) } }
end

#construct_hash(default = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/monkey/ext/enumerable.rb', line 9

def construct_hash(default = {})
  enum_for :construct_hash unless block_given? or !respond_to? :enum_for
  construct(default.to_hash.dup) do |h,v|
    result = yield(v)
    result = [result, nil] unless result.is_a? Enumerable
    result = [result] unless result.respond_to? :to_ary
    h.merge! Hash[*result]
  end
end