array_utils 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/array_utils.rb +58 -0
  3. metadata +8 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d956d55c007e0d1ea35d91c3d08abbb73cb24245f48bc180b732fee2d4240e3d
4
- data.tar.gz: c5ada0a36e1b7f5d5c449db8bf57e1c27cc93903ed57b8ca2316f7243cf493ad
3
+ metadata.gz: 9328e017c9d5987494e2d6de4ef7c2fdc626c0b351ea7cddaa9a55e6482ec1bc
4
+ data.tar.gz: 06dd470410326a939e0ee7f5cafe7e7c8e6c8985df17334a65a0875b03c36662
5
5
  SHA512:
6
- metadata.gz: e217bc0f93e6bf34e584c4a780c4800d4dd23b4778e1284a3d8ceba2af898c4b4ecb1a60547249a58e5678d51eb53ae8c0fb4be02c7adda13651a0a69f759442
7
- data.tar.gz: 8209e03eae1a309aebbfaa72b25c698d96413c27722299a20b066cebeb24a2ec2c62d5724f6fc087465a5413b65b4c2195d3488809af9c31a0d89718e13aab43
6
+ metadata.gz: 4af0682ffe381b43b60d824a1e45ddfdb0c0990707c3c638c3eb5ce79972332e2bbef39d6b610d80dd100e679a329535795dcfc09da1be1e0e622efefbcb3fd4
7
+ data.tar.gz: ae6be72e010f74407247aceadfb17ae93b1b2bf23c560da8be5d65ef2800130b9f4a37b8faf0e0ae6a5df0e3e403626e816b12d400df32de53126396468e5762
data/lib/array_utils.rb CHANGED
@@ -25,4 +25,62 @@ class Array
25
25
 
26
26
  partition(&:odd?)
27
27
  end
28
+
29
+ # Determines whether an array is "sparse," meaning it contains a significant
30
+ # proportion
31
+ # of `nil` values compared to its size.
32
+ #
33
+ # @param threshold [Float] The threshold ratio (default: 0.5).
34
+ # If the proportion of `nil` values to the array's size exceeds this
35
+ # threshold, the array is considered sparse.
36
+ #
37
+ # @return [Boolean] `true` if the array is sparse, `false` otherwise.
38
+ #
39
+ # @example Basic usage:
40
+ # [1, nil, nil, 4, nil].sparse? # => true (more than 50% nil values)
41
+ #
42
+ # @example Custom threshold:
43
+ # [1, nil, nil, 4, 5].sparse?(0.8) # => false (less than 80% nil values)
44
+ #
45
+ # @example Non-sparse array:
46
+ # [1, 2, 3, 4, 5].sparse? # => false (no nil values)
47
+ def sparse?(threshold = 0.5)
48
+ flatten.count(nil).to_f / flatten.size > threshold
49
+ end
50
+
51
+ # Returns a new array where all `nil` values are removed.
52
+ #
53
+ # If an element is an array, it is compacted (removing `nil` values)
54
+ # before being added. This makes it more effective than the default `#compact`
55
+ # method which only works with 1D arrays.
56
+ #
57
+ # @return [Array] A new array with `nil` values removed.
58
+ def densify
59
+ densified_array = []
60
+
61
+ each do |elem|
62
+ next unless elem
63
+
64
+ if elem.is_a? Array
65
+ nested = elem.densify
66
+ # skip empty arrays after removing nil values
67
+ densified_array << nested unless nested.empty?
68
+ else
69
+ densified_array << elem
70
+ end
71
+ end
72
+
73
+ densified_array
74
+ end
75
+
76
+ # Modifies the array in place by removing all `nil` values.
77
+ #
78
+ # If an element is an array, it is compacted (removing `nil` values)
79
+ # before being added. This makes it more effective than the default `#compact`
80
+ # method which only works with 1D arrays.
81
+ #
82
+ # @return [self] The modified array.
83
+ def densify!
84
+ replace(densify)
85
+ end
28
86
  end
metadata CHANGED
@@ -1,24 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: array_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxwell Nana Forson
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-03-23 00:00:00.000000000 Z
10
+ date: 2025-03-26 00:00:00.000000000 Z
11
11
  dependencies: []
12
- description: |
13
- The default Ruby Array class allows inserting into an array at an that
14
- doesn't exist. For example, a user may do this `array[2938] = 48` when the
15
- array contains only about six (6) elements. This causes the the array to now
16
- contain 2,000 plus of nil elements. The goal is to avoid this by intelligently
17
- appending to the end of the array when such action is performed.
12
+ description: |2
13
+ A simple Ruby gem that handles the issue with Ruby’s Array class when
14
+ inserting an element at an index greater than the current array size.
15
+ It handles this by intelligently appending the result to the end of the
16
+ array instead of creating a sparse array.
18
17
 
19
- Also, invalid negative indexes will throw an `IndexError` exception as usual.
20
-
21
- There are other useful methods added on top of the Array class for common tasks.
18
+ It also includes some other useful Array methods for common uses.
22
19
  email: maxwellnanaforson@gmail.com
23
20
  executables: []
24
21
  extensions: []