Module: Enumerable

Defined in:
lib/ruby_core_extensions/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#detect_and_returnObject



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

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

#map_methods(*methods) ⇒ Object



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

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



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

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

#with_object(obj, &block) ⇒ Object



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

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