Module: Enumerable

Defined in:
lib/ruby_core_extensions/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#detect_and_returnObject



13
14
15
16
17
18
# File 'lib/ruby_core_extensions/enumerable.rb', line 13

def detect_and_return
  detect do |e|
    result = yield(e)
    return result if result
  end
end

#map_methods(*methods) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/ruby_core_extensions/enumerable.rb', line 3

def map_methods(*methods)
  map do |object|
    methods.inject({}) do |h, method|
      h[method] = object.send(method)
      h
    end
  end
end

#select_by_attr(attr, value) ⇒ Object



21
22
23
24
25
# File 'lib/ruby_core_extensions/enumerable.rb', line 21

def select_by_attr(attr, value)
  select do |e|
    e.send(attr) == value
  end
end

#with_object(obj, &block) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/ruby_core_extensions/enumerable.rb', line 29

def with_object(obj, &block)
  return to_enum :with_object, obj unless block_given?
  each do |element|
    yield element, obj
  end
  obj
end