Module: WrapIt::CaptureArray

Defined in:
lib/wrap_it/capture_array.rb

Overview

Adds #capture! and #capture_first! methods to array. Theese methods are extracts items from array by some conditions and returns its as separate array for #capture! and as first item for #capture_first!.

Author:

Constant Summary collapse

REQUIRED_METHODS =
%i(reject! find_index delete_at)

Instance Method Summary collapse

Instance Method Details

#capture!([condition, ...], opts = {}) ⇒ Array #capture! {|element| ... } ⇒ Array

Extracts elements from array by conditions, passed in arguments and returns theese elements as new array.

Condition can be Regexp, Class, Array, lambdas and any other value. if condition contains labdas, all off them will be called before tests and results of theese calls will be used as conditions.

If condition is Regexp, all elements of array are tested for matching to this regexp, previously converted to String by their to_s method. If condition is an Array, all elements tested if it included in these array. If the condition is a class, then elements are tested via is_a? method for this class. true and false conditions do exactly what it mean - true will satisfy condition, false will not. For any other value, elements are tested with equality operator ==.

You can provide a block. In this case, all arguments are ignored, and block yielded for each element of array. If block returns true, element extracted from array.

All conditions, passed as arguments are or-ed so String, Symbol means select Symbol or String elements.

You can also specify and option, so all tests will be and'ed with its conditions.

Examples:

capture by class

arr = [1, 2, 3, 'and', 'string']
arr.extend WrapIt::CaptureArray
arr.capture(String) #=> ['and', 'string']
arr                 #=> [1, 2, 3]

capture by value

arr = [1, 2, 3, 'and', 'string']
arr.extend WrapIt::CaptureArray
arr.capture(1, 2) #=> [1, 2]
arr               #=> [3, 'and', 'string']

capture by Regexp

arr = [1, 2, 3, 'and', 'string', :str]
arr.extend WrapIt::CaptureArray
arr.capture(/^str/) #=> ['string', :str]
arr                 #=> [1, 2, 3, 'and']

capture by Array

arr = [1, 2, 3, 'and', 'string']
arr.extend WrapIt::CaptureArray
arr.capture([1, 10, 'and']) #=> [1, 'and']
arr                         #=> [2, 3, 'string']

capture by block

arr = [1, 2, 3, 'and', 'string']
arr.extend WrapIt::CaptureArray
arr.capture {|x| x < 3} #=> [1, 2]
arr                     #=> [3, 'and', 'string']

capture with and condition

arr = [1, 2, 3, 'and', 'string', :str]
arr.extend WrapIt::CaptureArray
arr.capture(String, and: [/^str/]) #=> ['string']
arr                                #=> [1, 2, 3, 'and', :str]

Overloads:

  • #capture!([condition, ...], opts = {}) ⇒ Array

    Parameters:

    • condition (Object)

      one of or-ed conditions for comparing

    • opts (Hash) (defaults to: {})

      options for extracting

    Options Hash (opts):

    • :and (Object, Array)

      one or array of and-ed conditions

  • #capture! {|element| ... } ⇒ Array

    Yields:

    • (element)

      Gives each element of array to block. You should return true to capture this element or false to keep it in array.

    Yield Parameters:

    • element (Object)

      element of array to inspect

    Yield Returns:

    • (Boolean)

      whether exclude this element or not

Returns:

  • (Array)

    array of captured elements



97
98
99
100
101
102
103
# File 'lib/wrap_it/capture_array.rb', line 97

def capture!(*args, &block)
  captureed = []
  reject! do |arg|
    do_compare(arg, *args, &block) && (captureed << arg) && true
  end
  captureed
end

#capture_first!(*args, &block) ⇒ Object

Extracts first element from array that is satisfy conditions, passed in arguments and returns these element.

See Also:



110
111
112
113
# File 'lib/wrap_it/capture_array.rb', line 110

def capture_first!(*args, &block)
  index = find_index { |arg| do_compare(arg, *args, &block) }
  index.nil? ? nil : delete_at(index)
end