Sha256: 91191eff0e8b8d5185fe37a75d68e6bf13188212690b36ad87adabe0797f6d05

Contents?: true

Size: 927 Bytes

Versions: 3

Compression:

Stored size: 927 Bytes

Contents

module Enumerable

  # Yield each element to the block and return the result
  # of the block when that result evaluates as true,
  # terminating early like #detect and #find.
  # 
  #   obj1.foo? #=> false
  #   obj2.foo? #=> true
  #   obj2.foo  #=> "value"
  #
  #   [obj1, obj2].find_yield{ |obj| obj.foo if obj.foo? }     #=> "value"
  #
  # Another example:
  #
  #   [1,2,3,4,5].find_yield{ |i| j = i+1; j if j % 4 == 0 }   #=> "5"
  #
  # If the block is never true, return the object given in the first parameter,
  # or nil if none specified.
  #
  #   [1,2,3].find_yield{ |_| false }    #=> nil
  #   [false].find_yield(1){ |_| false } #=> 1
  #
  def find_yield(fallback=nil) #:yield:
    each do |member|
      result = yield(member)
      return result if result 
    end
    fallback
  end

  # DEPRECATE: This has been renamed to #find_yield.
  def map_detect(fallback=nil)
    find_yield(fallback)
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/enumerable/find_yield.rb
facets-2.8.3 lib/core/facets/enumerable/find_yield.rb
facets-2.8.2 lib/core/facets/enumerable/find_yield.rb