Wednesday, September 29, 2021

Array Include Methods 1.4.0

The array_include_methods gem (Array#include_all? & Array#include_any? methods missing from basic Ruby Array API and more!) has had several releases in versions 1.1.0-1.4.0, with the following changes (some API breaking):

1.4.0

  • array_diff_indexes(other_array) (alias: array_diff_indices)
  • array_intersection_indexes(other_array) (alias: array_intersection_indices)

1.3.0

  • [API Breaking] Separate between operations include_any?(*array) and include_any?(array) to avoid confusion (remove support for the latter as it is not necessary)
  • [API Breaking] Separate between operations include_all?(*array) and include_all?(array) to avoid confusion (rename the latter to include_array?(array))
  • Array#include_all?(*other_array, same_sort: true) accepts same_sort option (default: false)

1.2.0

  • Add Array#array_index(array) method to determine start array index of other array

1.1.0

  • Perform contiguous-element include_all?([...]) check against an array argument (not splatted)
  • Perform sorted include_all?([...]) check against an array argument (not splatted)
  • Perform non-repetition include_all?([...]) check against an array argument (not splatted)
Here is a reminder of what the gem is all about:


ArrayIncludeMethods

Array#include_all? & Array#include_any? methods missing from basic Ruby Array API and more!

Examples

Array#include_any?(*other_array)

[1, 2, 3, 4].include_any?(2, 4, 5) # returns true
[1, 2, 3, 4].include_any?(6, 7) # returns false
[1, 2, 3, 4].include_any?() # returns true
[1, 2, 3, 4].include_any?(nil) # returns false

Array#include_all?(*other_array)

[1, 2, 3, 4].include_all?(2, 3) # returns true
[1, 2, 3, 4].include_all?(2, 4) # returns true
[1, 2, 3, 4].include_all?(4, 2) # returns true
[1, 2, 3, 4].include_all?(4, 2, same_sort: true) # returns false
[1, 2, 3, 4].include_all?(2, 4, 4) # returns true
[1, 2, 3, 4].include_all?(2, 4, 5) # returns false
[1, 2, 3, 4].include_all?() # returns true
[1, 2, 3, 4].include_all?(nil) # returns false

Array#include_array?(other_array)

[1, 2, 3, 4].include_array?([2, 3]) # returns true
[1, 2, 3, 4].include_array?([2, 4]) # returns false
[1, 2, 3, 4].include_array?([4, 2]) # returns false
[1, 2, 3, 4].include_array?([2, 4, 4]) # returns false
[1, 2, 3, 4].include_array?([2, 4, 5]) # returns false
[1, 2, 3, 4].include_array?([]) # returns true
[1, 2, 3, 4].include_array?([nil]) # returns false

Array#array_index(other_array)

Returns first array index of other_array in first_array assuming first_array.include_all?(other_array) returns true

[1, 2, 3, 4].array_index([2, 3, 4]) # returns 1
[1, 2, 3, 4].array_index([2, 3]) # returns 1
[1, 2, 3, 4].array_index([3, 4]) # returns 2
[1, 2, 3, 4].array_index([2, 4]) # returns -1
[1, 2, 3, 4].array_index([4, 2]) # returns -1
[1, 2, 3, 4].array_index([2, 4, 5]) # returns -1
[1, 2, 3, 4].array_index([]) # returns -1
[1, 2, 3, 4].array_index(nil) # returns -1

Array#array_intersection_indexes(other_array)

(alias: Array#array_intersection_indices(other_array))

Returns indexes from self array for which elements match elements in other_array assuming same sort

[1, 2, 3, 4].array_intersection_indexes([2, 3, 4]) # returns [1, 2, 3]
[1, 2, 3, 4].array_intersection_indexes([2, 3]) # returns [1, 2]
[1, 2, 3, 4].array_intersection_indexes([3, 4]) # returns [2, 3]
[1, 2, 3, 4].array_intersection_indexes([2, 4]) # returns [1, 3]
[1, 2, 3, 4].array_intersection_indexes([4, 2]) # returns [3]
[1, 2, 3, 4].array_intersection_indexes([2, 4, 5]) # returns [1, 3]
[1, 2, 3, 4].array_intersection_indexes([]) # returns []
[1, 2, 3, 4].array_intersection_indexes(nil) # returns []

Array#array_diff_indexes(other_array)

(alias: Array#array_diff_indices(other_array))

Returns indexes from self array for which elements do not match elements in other_array assuming same sort

[1, 2, 3, 4].array_diff_indexes([2, 3, 4]) # returns [0]
[1, 2, 3, 4].array_diff_indexes([2, 3]) # returns [0, 3]
[1, 2, 3, 4].array_diff_indexes([3, 4]) # returns [0, 1]
[1, 2, 3, 4].array_diff_indexes([2, 4]) # returns [0, 2]
[1, 2, 3, 4].array_diff_indexes([4, 2]) # returns [0, 1, 2]
[1, 2, 3, 4].array_diff_indexes([2, 4, 5]) # returns [0, 2]
[1, 2, 3, 4].array_diff_indexes([]) # returns [0, 1, 2, 3]
[1, 2, 3, 4].array_diff_indexes(nil) # returns [0, 1, 2, 3]

No comments: