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

[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



307
308
309
310
311
312
313
# File 'lib/ruber/utils.rb', line 307

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