module Enumerable

Instance methods to extend {Enumerable}.

Public Instance Methods

/(proc_able) click to toggle source

@note EXPERIMENTAL!

An idea I'm playing around with for convenient mapping of {Enumerable}.

@example Extract Attributes

Cat   = Struct.new :name, :breed

cats  = [ Cat.new( 'Hudie', 'Chinese-American Shorthair' ),
          Cat.new( 'Oscar', 'Bengal' ) ]

cats/:name  #=> [ 'Hudie', 'Oscar' ]
cats/:breed #=> [ 'Chinese-American Shorthair', 'Bengal' ]

@example Extract Values

# Need the array.to_proc ~> ->( key ) { array.dig *key } for it to really
# *feel* nice... and that's the whole point!
require 'nrser/core_ext/array/to_proc'

kitties = [ { name: 'Hootie' }, 
            { name: 'Oscie'  } ]

kitties/[:name] #=> [ 'Hooie', 'Oscie' ]

Not so bad, eh? I'm calling it “slash-map” for the moment, BTW.

@param [#to_proc] proc_able

Something that can be `#to_proc`'d for the {Enumerable#map}.

@return [Enumerable]

# File lib/nrser/core_ext/enumerable/slash_map.rb, line 33
def / proc_able
  map &proc_able
end
assoc_by(*args, &block) click to toggle source

See {NRSER.assoc_by}

# File lib/nrser/core_ext/enumerable.rb, line 35
def assoc_by *args, &block
  NRSER.assoc_by self, *args, &block
end
assoc_to(*args, &block) click to toggle source

See {NRSER.assoc_to}

# File lib/nrser/core_ext/enumerable.rb, line 41
def assoc_to *args, &block
  NRSER.assoc_to self, *args, &block
end
count_by(&block) click to toggle source

See {NRSER.count_by}

# File lib/nrser/core_ext/enumerable.rb, line 65
def count_by &block
  NRSER.count_by self, &block
end
enumerate_as_values() click to toggle source

See {NRSER.enumerate_as_values}

# File lib/nrser/core_ext/enumerable.rb, line 47
def enumerate_as_values
  NRSER.enumerate_as_values self
end
find_bounded(bounds, &block) click to toggle source

See {NRSER.find_bounded}

# File lib/nrser/core_ext/enumerable.rb, line 11
def find_bounded bounds, &block
  NRSER.find_bounded self, bounds, &block
end
find_map(ifnone = nil, &block) click to toggle source

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

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

@example

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

@param [nil | Proc<()=>DEFAULT>] ifnone

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

@param [Proc<(E)=>RESLUT>] block

Block mapping entires to results.

@return [nil]

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

@return [V]

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

@return [RESULT]

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

@return [DEFAULT]

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

@return [nil]

When `ifnone` procedure is *not* provided and `&block` returns `nil` or
`false` for all entries.
# 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) click to toggle source

See {NRSER.find_only}

# File lib/nrser/core_ext/enumerable.rb, line 17
def find_only &block
  NRSER.find_only self, &block
end
find_only!(&block) click to toggle source

Right now, exactly the same as {#find_only}… though I wished I had called it this and had {#find_only} return `nil` if it failed, as is kind-of a some-what established practice, because now I get confused.

Maybe some day I will make that change. For now, this is here so when I forget and add the `!` it works.

# File lib/nrser/core_ext/enumerable.rb, line 29
def find_only! &block
  NRSER.find_only self, &block
end
only(**options) click to toggle source

Calls {NRSER.only} on `self`.

# File lib/nrser/core_ext/enumerable.rb, line 53
def only **options
  NRSER.only self, **options
end
only!() click to toggle source

See {NRSER.only!}

# File lib/nrser/core_ext/enumerable.rb, line 59
def only!
  NRSER.only! self
end
slice?(*args, &block) click to toggle source

See {NRSER.slice?}

# File lib/nrser/core_ext/enumerable.rb, line 77
def slice? *args, &block
  NRSER.slice? self, *args, &block
end
try_find(&block) click to toggle source

See {NRSER.try_find}

# File lib/nrser/core_ext/enumerable.rb, line 71
def try_find &block
  NRSER.try_find self, &block
end