array_utils 0.0.1

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 +7 -0
  2. data/lib/array_utils.rb +28 -0
  3. metadata +49 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d956d55c007e0d1ea35d91c3d08abbb73cb24245f48bc180b732fee2d4240e3d
4
+ data.tar.gz: c5ada0a36e1b7f5d5c449db8bf57e1c27cc93903ed57b8ca2316f7243cf493ad
5
+ SHA512:
6
+ metadata.gz: e217bc0f93e6bf34e584c4a780c4800d4dd23b4778e1284a3d8ceba2af898c4b4ecb1a60547249a58e5678d51eb53ae8c0fb4be02c7adda13651a0a69f759442
7
+ data.tar.gz: 8209e03eae1a309aebbfaa72b25c698d96413c27722299a20b066cebeb24a2ec2c62d5724f6fc087465a5413b65b4c2195d3488809af9c31a0d89718e13aab43
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Array
4
+ alias_method :original_set, :[]=
5
+
6
+ def []=(index_or_range, length = nil, value)
7
+ if length.nil?
8
+ if index_or_range.is_a?(Integer) && index_or_range >= size
9
+ push(value)
10
+ elsif index_or_range.is_a?(Range)
11
+ original_set(index_or_range, value) # Handle range assignment
12
+ else
13
+ original_set(index_or_range, value)
14
+ end
15
+ else
16
+ original_set(index_or_range, length, value) # Handle slice assignment
17
+ end
18
+ end
19
+
20
+ # Partitions an array of integers based on parity (odd or even property)
21
+ #
22
+ # @return [Array] An nested array of odd and even numbers.
23
+ def split_by_parity
24
+ raise TypeError, "Expected only numbers" unless all?(Numeric)
25
+
26
+ partition(&:odd?)
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: array_utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Maxwell Nana Forson
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-03-23 00:00:00.000000000 Z
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.
18
+
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.
22
+ email: maxwellnanaforson@gmail.com
23
+ executables: []
24
+ extensions: []
25
+ extra_rdoc_files: []
26
+ files:
27
+ - lib/array_utils.rb
28
+ homepage: https://github.com/nanafox/array_utils
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '1.9'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.6.6
47
+ specification_version: 4
48
+ summary: Useful array methods and out-of-bounds index handler
49
+ test_files: []