Module: Enumerable

Instance Method Summary collapse

Instance Method Details

#find! { ... } ⇒ Object? Also known as: find_and_map

Finds the first element for which the block returns a true value and returns the value of the block

It works like @Enumerable#find@, but, instead of returning the element for which the block returned a true value, it returns the value returned by the block. if the block returns nil or false for all elements

Examples:

Using @find!@

[1, 2, 3, 4].find!{|i| 2*i if i > 2}
=> 6 #(3*2)

Yields:

  • obj

Returns:

  • (Object, nil)

    the first non-false value returned by the block or nil



316
317
318
319
320
321
322
# File 'lib/ruber/utils.rb', line 316

def find!
  each do |obj|
    res = yield obj
    return res if res
  end
  nil
end