Module: Enumerable

Included in:
NRSER::Sys::Env::Path
Defined in:
lib/nrser/core_ext/enumerable.rb,
lib/nrser/core_ext/enumerable/find_map.rb

Overview

Instance methods to extend Enumerable.

Instance Method Summary collapse

Instance Method Details

#assoc_by(*args, &block) ⇒ Object



21
22
23
# File 'lib/nrser/core_ext/enumerable.rb', line 21

def assoc_by *args, &block
  NRSER.assoc_by self, *args, &block
end

#assoc_to(*args, &block) ⇒ Object



27
28
29
# File 'lib/nrser/core_ext/enumerable.rb', line 27

def assoc_to *args, &block
  NRSER.assoc_to self, *args, &block
end

#count_by(&block) ⇒ Object



51
52
53
# File 'lib/nrser/core_ext/enumerable.rb', line 51

def count_by &block
  NRSER.count_by self, &block
end

#enumerate_as_valuesObject



33
34
35
# File 'lib/nrser/core_ext/enumerable.rb', line 33

def enumerate_as_values
  NRSER.enumerate_as_values self
end

#find_bounded(bounds, &block) ⇒ Object



9
10
11
# File 'lib/nrser/core_ext/enumerable.rb', line 9

def find_bounded bounds, &block
  NRSER.find_bounded self, bounds, &block
end

#find_map(ifnone = nil, &block) ⇒ nil, ...

Find the first truthy (not ‘nil` or `false`) result of calling `&block` on entries.

Like #find, accepts an optional ‘ifnone` procedure to call if no match is found.

Examples:


[1, 2, 3, 4].find_map do |i|
  if i.even?
    "#{ i } is even!"
  end
end
# => "2 is even!"

Parameters:

  • ifnone (nil | Proc<()=>DEFAULT>) (defaults to: nil)

    Optional lambda to call for the return value when no match is found.

  • &block (Proc<(E)=>RESLUT>)

    Block mapping entires to results.

Returns:

  • (nil)

    When ‘block.call( E )` is `nil` or `false` for all entries `E` and `ifnone` is `nil` or not provided.

  • (V)

    When ‘block.call( E )` is `nil` or `false` for all entries `E` and `ifnone` is a lambda that returns `DEFAULT`.

  • (RESULT)

    The first result ‘RESLUT = block.call( E )` where `RESLUT` is not `nil` or `false`.

  • (DEFAULT)

    When ‘ifnone` procedure is provided and `&block` returns `nil` or `false` for all entries.

  • (nil)

    When ‘ifnone` procedure is not provided and `&block` returns `nil` or `false` for all entries.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nrser/core_ext/enumerable/find_map.rb', line 44

def find_map ifnone = nil, &block
  each do |entry|
    if result = block.call( entry )
      # Found a match, short-circuit
      return result
    end
  end
  
  # No matches, return `ifnone`
  ifnone.call if ifnone
end

#find_only(&block) ⇒ Object



15
16
17
# File 'lib/nrser/core_ext/enumerable.rb', line 15

def find_only &block
  NRSER.find_only self, &block
end

#only(**options) ⇒ Object

Calls NRSER.only on ‘self`.



39
40
41
# File 'lib/nrser/core_ext/enumerable.rb', line 39

def only **options
  NRSER.only self, **options
end

#only!Object



45
46
47
# File 'lib/nrser/core_ext/enumerable.rb', line 45

def only!
  NRSER.only! self
end

#slice?(*args, &block) ⇒ Boolean

See NRSER.slice?

Returns:

  • (Boolean)


63
64
65
# File 'lib/nrser/core_ext/enumerable.rb', line 63

def slice? *args, &block
  NRSER.slice? self, *args, &block
end

#try_find(&block) ⇒ Object



57
58
59
# File 'lib/nrser/core_ext/enumerable.rb', line 57

def try_find &block
  NRSER.try_find self, &block
end