array_include_methods 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +18 -12
- data/VERSION +1 -1
- data/array_include_methods.gemspec +5 -3
- data/lib/array_include_methods.rb +30 -33
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f893cf42569d8afb770fc426df2ad336d81f32af27a9c3793960288ffba9da90
|
4
|
+
data.tar.gz: 2f68fec3b44267052eca66fdecf5b9507e8c80774210a6e93a1754ef9e44ff05
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84634ac0899e45f2d48f28de1c3ee849b4c78c1691bcbf748e955595ee95c3dd4d54e12b2c7c07d01815f23cda0cfcc18e8959beb716559366c584b5542b6568
|
7
|
+
data.tar.gz: 93928af74b96981a77b1163b92cd4f2106b98cfcf96e0b9d3aeb8b463f426c2cf3fee6caa8e6d95e2be6052837bc785c6a73f886975b057bac9b52424c57e186
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
## 1.3.0
|
4
|
+
|
5
|
+
- [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)
|
6
|
+
- [API Breaking] Separate between operations `include_all?(*array)` and `include_all?(array)` to avoid confusion (rename the latter to `include_array?(array)`)
|
7
|
+
- `Array#include_all?(*other_array, same_sort: true)` accepts `same_sort` option (default: `false`)
|
8
|
+
|
3
9
|
## 1.2.0
|
4
10
|
|
5
11
|
- Add `Array#array_index(array)` method to determine start array index of other array
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# ArrayIncludeMethods 1.
|
1
|
+
# ArrayIncludeMethods 1.3.0 - Ruby Refinement
|
2
2
|
[![Gem Version](https://badge.fury.io/rb/array_include_methods.svg)](http://badge.fury.io/rb/array_include_methods)
|
3
3
|
[![Build Status](https://travis-ci.com/AndyObtiva/array_include_methods.svg?branch=master)](https://travis-ci.com/AndyObtiva/array_include_methods)
|
4
4
|
[![Coverage Status](https://coveralls.io/repos/github/AndyObtiva/array_include_methods/badge.svg?branch=master)](https://coveralls.io/github/AndyObtiva/array_include_methods?branch=master)
|
@@ -12,7 +12,7 @@
|
|
12
12
|
Include the following in Gemfile:
|
13
13
|
|
14
14
|
```ruby
|
15
|
-
gem 'array_include_methods', '~> 1.
|
15
|
+
gem 'array_include_methods', '~> 1.3.0'
|
16
16
|
```
|
17
17
|
|
18
18
|
Run:
|
@@ -26,7 +26,7 @@ bundle
|
|
26
26
|
Run:
|
27
27
|
|
28
28
|
```
|
29
|
-
gem install array_include_methods -v1.
|
29
|
+
gem install array_include_methods -v1.3.0
|
30
30
|
```
|
31
31
|
|
32
32
|
## Usage
|
@@ -51,10 +51,8 @@ Now, you have `#include_all?` and `#include_any?` methods on `Array` objects.
|
|
51
51
|
|
52
52
|
```ruby
|
53
53
|
[1, 2, 3, 4].include_any?(2, 4, 5) # returns true
|
54
|
-
[1, 2, 3, 4].include_any?([2, 4, 5]) # returns true
|
55
54
|
[1, 2, 3, 4].include_any?(6, 7) # returns false
|
56
|
-
[1, 2, 3, 4].include_any?(
|
57
|
-
[1, 2, 3, 4].include_any?([]) # returns true
|
55
|
+
[1, 2, 3, 4].include_any?() # returns true
|
58
56
|
[1, 2, 3, 4].include_any?(nil) # returns false
|
59
57
|
```
|
60
58
|
|
@@ -62,19 +60,27 @@ Now, you have `#include_all?` and `#include_any?` methods on `Array` objects.
|
|
62
60
|
|
63
61
|
```ruby
|
64
62
|
[1, 2, 3, 4].include_all?(2, 3) # returns true
|
65
|
-
[1, 2, 3, 4].include_all?([2, 3]) # returns true
|
66
63
|
[1, 2, 3, 4].include_all?(2, 4) # returns true
|
67
|
-
[1, 2, 3, 4].include_all?([2, 4]) # returns false (checks true array containment)
|
68
64
|
[1, 2, 3, 4].include_all?(4, 2) # returns true
|
69
|
-
[1, 2, 3, 4].include_all?(
|
65
|
+
[1, 2, 3, 4].include_all?(4, 2, same_sort: true) # returns false
|
70
66
|
[1, 2, 3, 4].include_all?(2, 4, 4) # returns true
|
71
|
-
[1, 2, 3, 4].include_all?([2, 4, 4]) # returns false (checks true array containment)
|
72
67
|
[1, 2, 3, 4].include_all?(2, 4, 5) # returns false
|
73
|
-
[1, 2, 3, 4].include_all?(
|
74
|
-
[1, 2, 3, 4].include_all?([]) # returns true
|
68
|
+
[1, 2, 3, 4].include_all?() # returns true
|
75
69
|
[1, 2, 3, 4].include_all?(nil) # returns false
|
76
70
|
```
|
77
71
|
|
72
|
+
### `Array#include_array?(other_array)`
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
[1, 2, 3, 4].include_array?([2, 3]) # returns true
|
76
|
+
[1, 2, 3, 4].include_array?([2, 4]) # returns false
|
77
|
+
[1, 2, 3, 4].include_array?([4, 2]) # returns false
|
78
|
+
[1, 2, 3, 4].include_array?([2, 4, 4]) # returns false
|
79
|
+
[1, 2, 3, 4].include_array?([2, 4, 5]) # returns false
|
80
|
+
[1, 2, 3, 4].include_array?([]) # returns true
|
81
|
+
[1, 2, 3, 4].include_array?([nil]) # returns false
|
82
|
+
```
|
83
|
+
|
78
84
|
### `Array#array_index(other_array)`
|
79
85
|
|
80
86
|
Returns first array index of `other_array` in `first_array` assuming `first_array.include_all?(other_array)` returns true
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.3.0
|
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: array_include_methods 1.
|
5
|
+
# stub: array_include_methods 1.3.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "array_include_methods".freeze
|
9
|
-
s.version = "1.
|
9
|
+
s.version = "1.3.0"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib".freeze]
|
13
13
|
s.authors = ["Andy Maleh".freeze]
|
14
|
-
s.date = "2021-09-
|
14
|
+
s.date = "2021-09-26"
|
15
15
|
s.description = "Array#include_all? & Array#include_any? methods missing from basic Ruby Array API".freeze
|
16
16
|
s.email = "andy.am@gmail.com".freeze
|
17
17
|
s.extra_rdoc_files = [
|
@@ -43,6 +43,7 @@ Gem::Specification.new do |s|
|
|
43
43
|
s.add_development_dependency(%q<coveralls>.freeze, ["= 0.8.23"])
|
44
44
|
s.add_development_dependency(%q<simplecov>.freeze, ["~> 0.16.1"])
|
45
45
|
s.add_development_dependency(%q<simplecov-lcov>.freeze, ["~> 0.7.0"])
|
46
|
+
s.add_development_dependency(%q<puts_debuggerer>.freeze, ["> 0"])
|
46
47
|
s.add_development_dependency(%q<rake-tui>.freeze, ["> 0"])
|
47
48
|
else
|
48
49
|
s.add_dependency(%q<rspec>.freeze, ["~> 3.5.0"])
|
@@ -51,6 +52,7 @@ Gem::Specification.new do |s|
|
|
51
52
|
s.add_dependency(%q<coveralls>.freeze, ["= 0.8.23"])
|
52
53
|
s.add_dependency(%q<simplecov>.freeze, ["~> 0.16.1"])
|
53
54
|
s.add_dependency(%q<simplecov-lcov>.freeze, ["~> 0.7.0"])
|
55
|
+
s.add_dependency(%q<puts_debuggerer>.freeze, ["> 0"])
|
54
56
|
s.add_dependency(%q<rake-tui>.freeze, ["> 0"])
|
55
57
|
end
|
56
58
|
end
|
@@ -8,49 +8,46 @@ module ArrayIncludeMethods
|
|
8
8
|
# Returns `true` if all of the given `array` elements are present in `self`,
|
9
9
|
# otherwise returns `false`
|
10
10
|
# Always returns `true` if the given `array` is empty
|
11
|
-
#
|
12
|
-
def include_all?(*array)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
# `same_sort` option indicates that array must have the same sort as `self`. By default, it is `false`
|
12
|
+
def include_all?(*array, same_sort: false)
|
13
|
+
return false if array.size > self.size
|
14
|
+
self_copy = self.dup
|
15
|
+
array_copy = array.dup
|
16
|
+
book_keeping_array_copy = array.dup
|
17
|
+
self_element_index = last_element_index = -1
|
18
|
+
array_copy.each do |element|
|
19
|
+
if self_copy.include?(element)
|
20
|
+
last_element_index = self_element_index
|
21
|
+
self_element_index = self_copy.index(element)
|
22
|
+
return false if same_sort && self_element_index < last_element_index
|
23
|
+
self_copy.delete_at(self_element_index)
|
24
|
+
book_keeping_array_copy.delete(element)
|
25
|
+
else
|
26
|
+
return false
|
27
|
+
end
|
17
28
|
end
|
29
|
+
book_keeping_array_copy.empty?
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns `true` if the given `array` is present in `self` (in the same element order without repetition)
|
33
|
+
# Always returns `true` if the given `array` is empty
|
34
|
+
# Always returns `false` if the given `array` is nil
|
35
|
+
def include_array?(array)
|
18
36
|
return false if array.nil?
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
size_diff = a1.size - a2.size
|
26
|
-
(size_diff + 1).times.any? do |start_index|
|
27
|
-
a1[start_index, a2.size] == a2
|
28
|
-
end
|
29
|
-
end
|
30
|
-
else
|
31
|
-
(a1 & a2).uniq.sort == a2.uniq.sort
|
32
|
-
end
|
33
|
-
rescue ArgumentError => e
|
34
|
-
a2.uniq.all? { |element| a1.include?(element) }
|
37
|
+
if array.size > self.size
|
38
|
+
false
|
39
|
+
else
|
40
|
+
size_diff = self.size - array.size
|
41
|
+
(size_diff + 1).times.any? do |start_index|
|
42
|
+
self[start_index, array.size] == array
|
35
43
|
end
|
36
44
|
end
|
37
|
-
self_grouped_by = self.group_by(&:class)
|
38
|
-
array_grouped_by = array.group_by(&:class)
|
39
|
-
return false unless array_include_other_array_same_class_elements.call(self_grouped_by.keys.map(&:to_s), array_grouped_by.keys.map(&:to_s))
|
40
|
-
array_grouped_by.reduce(true) do |result, pair|
|
41
|
-
array_class = pair.first
|
42
|
-
array_elements = pair.last
|
43
|
-
self_grouped_by[array_class]
|
44
|
-
result && array_include_other_array_same_class_elements.call(self_grouped_by[array_class], array_elements)
|
45
|
-
end
|
46
45
|
end
|
47
46
|
|
48
47
|
# Returns `true` if any of the given `array` elements are present in `self`,
|
49
48
|
# otherwise returns `false`
|
50
49
|
# Always returns `true` if the given `array` is empty
|
51
|
-
# Always returns `false` if the given `array` is nil
|
52
50
|
def include_any?(*array)
|
53
|
-
array = array[0] if array.size == 1 && array[0].is_a?(Array)
|
54
51
|
!array.nil? && (array.empty? || !(self & array).empty?)
|
55
52
|
end
|
56
53
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: array_include_methods
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Maleh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-09-
|
11
|
+
date: 2021-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: 0.7.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: puts_debuggerer
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: rake-tui
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|