rb_core_extensions 0.1.0 → 0.1.2
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.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +1 -1
- data/CHANGELOG.md +19 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +3 -3
- data/README.md +73 -0
- data/Rakefile +3 -1
- data/bin/console +1 -1
- data/lib/core_extensions/all.rb +10 -0
- data/lib/core_extensions/array/delete.rb +22 -0
- data/lib/core_extensions/array/duplicates.rb +15 -0
- data/lib/core_extensions/array/inclusion.rb +67 -0
- data/lib/core_extensions/array/math.rb +55 -0
- data/lib/core_extensions/array.rb +8 -0
- data/lib/core_extensions/hash/access.rb +21 -0
- data/lib/core_extensions/hash/delete.rb +20 -0
- data/lib/core_extensions/hash/inclusion.rb +21 -0
- data/lib/core_extensions/hash.rb +7 -0
- data/lib/core_extensions/module.rb +14 -0
- data/lib/core_extensions/nil_class.rb +14 -0
- data/lib/core_extensions/object.rb +64 -0
- data/lib/core_extensions/version.rb +2 -2
- data/rb_core_extensions.gemspec +5 -3
- metadata +25 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff651ca46ca4e51a477160b362fdd434c6fa95d248d27a5022b0ee8f0c9b5b41
|
4
|
+
data.tar.gz: 819bafb17f44354587c741c2ede51e3bf7f002f7020bdb32a718d82016401519
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e05845abd3afc84fcee0fd1ee16d05f2b9904b5e9abe082cf261bddc2681e3637122786a3ddb792a6ba08273313ea766b8b4defcdbcf627de1ba747b333d10d2
|
7
|
+
data.tar.gz: 572c3a81cc8c4fe82b821220d3528c4497d4246b02061422ce7f1155af4427c70666db8b28f2a64a63e2f96a1523a3e57db8a46867e686ec0372a04444d57987
|
data/.github/workflows/main.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,22 @@
|
|
1
|
+
## [0.1.2](https://github.com/shivam091/core_extensions/compare/v0.1.1...v0.1.2) - 2023-07-06
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added core extension methods `keys?`, `slice!`, and `keys_at` in `Hash`
|
6
|
+
- Added core extension method `namespaces` in `Module`
|
7
|
+
- Added core extension method `blank?` in `NilClass`
|
8
|
+
- Added core extension methods `is_one_of?`, `deep_send`, and `in_namespace?` in `Object`
|
9
|
+
|
10
|
+
### What's changed
|
11
|
+
|
12
|
+
- Bumped version of `paambaati/codeclimate-action` workflow action to `5.0.0`
|
13
|
+
|
14
|
+
## [0.1.1](https://github.com/shivam091/core_extensions/compare/v0.1.0...v0.1.1) - 2023-07-05
|
15
|
+
|
16
|
+
### What's new
|
17
|
+
|
18
|
+
- Added core extension methods `take!`, `round`, `duplicates`, `include_all?`, `include_any?`, `include_none?`, `includes_index?`, `mean`, `stddev`, and `variance` in `Array`
|
19
|
+
|
1
20
|
## [0.1.0] - 2023-07-05
|
2
21
|
|
3
22
|
- Initial release
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rb_core_extensions (0.1.
|
5
|
-
activesupport
|
4
|
+
rb_core_extensions (0.1.2)
|
5
|
+
activesupport (~> 7.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
@@ -48,7 +48,7 @@ DEPENDENCIES
|
|
48
48
|
rake (~> 13.0)
|
49
49
|
rb_core_extensions!
|
50
50
|
rspec (~> 3.0)
|
51
|
-
simplecov (>= 0.21.2)
|
51
|
+
simplecov (~> 0.21, >= 0.21.2)
|
52
52
|
|
53
53
|
BUNDLED WITH
|
54
54
|
2.4.10
|
data/README.md
CHANGED
@@ -2,6 +2,13 @@
|
|
2
2
|
|
3
3
|
`rb_core_extensions` is a set of core extensions beyond those provided by `Ruby` and `ActiveSupport`.
|
4
4
|
|
5
|
+
[](https://github.com/shivam091/rb_core_extensions/actions/workflows/main.yml)
|
6
|
+
[](https://badge.fury.io/rb/rb_core_extensions)
|
7
|
+
[](http://rubygems.org/gems/rb_core_extensions)
|
8
|
+
[](https://codeclimate.com/github/shivam091/core_extensions/test_coverage)
|
9
|
+
[](https://codeclimate.com/github/shivam091/core_extensions/maintainability)
|
10
|
+
[](https://github.com/shivam091/core_extensions/blob/main/LICENSE.txt)
|
11
|
+
|
5
12
|
## Minimum Requirements
|
6
13
|
|
7
14
|
* Ruby 3.2.2+ (https://www.ruby-lang.org/en/downloads/branches/)
|
@@ -22,6 +29,72 @@ Or otherwise simply install it yourself as:
|
|
22
29
|
|
23
30
|
`$ gem install rb_core_extensions`
|
24
31
|
|
32
|
+
### Usage
|
33
|
+
|
34
|
+
Loading all core extensions at once:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
require "core_extensions/all"
|
38
|
+
```
|
39
|
+
|
40
|
+
Cherry-picking only what you want:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
require "core_extensions/array"
|
44
|
+
require "core_extensions/array/math"
|
45
|
+
require "core_extensions/date"
|
46
|
+
```
|
47
|
+
|
48
|
+
## Extensions Provided
|
49
|
+
|
50
|
+
Below are the extension methods which are provided by `rb_core_extensions`. Please refer [documentation](https://shivam091.github.io/core_extensions) or rspec for examples of how to use these methods.
|
51
|
+
|
52
|
+
### [Array](https://shivam091.github.io/core_extensions/Array.html)
|
53
|
+
|
54
|
+
#### ***core_extensions/array/delete.rb***
|
55
|
+
1. **[`#take!`](https://shivam091.github.io/core_extensions/Array.html#method-i-take-21)** - Alters the array by removing first `n` elements.
|
56
|
+
|
57
|
+
#### ***core_extensions/array/math.rb***
|
58
|
+
1. **[`round`](https://shivam091.github.io/core_extensions/Array.html#method-i-round)** - Rounds each element of the numeric array up to specified `precision`.
|
59
|
+
2. **[`mean`](https://shivam091.github.io/core_extensions/Array.html#method-i-mean)** - Returns the mean of the array of `Numeric`.
|
60
|
+
3. **[`variance`](https://shivam091.github.io/core_extensions/Array.html#method-i-variance)** - Returns the variance of the array of `Numeric`.
|
61
|
+
4. **[`stddev`](https://shivam091.github.io/core_extensions/Array.html#method-i-stddev)** - Returns the standard deviation of the array of `Numeric`.
|
62
|
+
|
63
|
+
#### ***core_extensions/array/duplicates.rb***
|
64
|
+
1. **[`duplicates`](https://shivam091.github.io/core_extensions/Array.html#method-i-duplicates)** - Returns an array of the duplicate elements.
|
65
|
+
|
66
|
+
#### ***core_extensions/array/inclusion.rb***
|
67
|
+
1. **[`include_any?`](https://shivam091.github.io/core_extensions/Array.html#method-i-include_any-3F)** - Returns whether the array contains any of the `elements`.
|
68
|
+
2. **[`include_none?`](https://shivam091.github.io/core_extensions/Array.html#method-i-include_none-3F)** - Returns whether the array contains none of the `elements`.
|
69
|
+
3. **[`include_all?`](https://shivam091.github.io/core_extensions/Array.html#method-i-include_all-3F)** - Returns whether the array contains all of the `elements`.
|
70
|
+
4. **[`includes_index?`](https://shivam091.github.io/core_extensions/Array.html#method-i-includes_index-3F)** - Returns whether the array has a value at the specified `index`.
|
71
|
+
|
72
|
+
### [Hash](https://shivam091.github.io/core_extensions/Hash.html)
|
73
|
+
|
74
|
+
#### ***core_extensions/hash/inclusion.rb***
|
75
|
+
1. **[`#keys?`](https://shivam091.github.io/core_extensions/Hash.html#method-i-keys-3F)** - Returns whether the hash contains all of the specified `keys`.
|
76
|
+
#### ***core_extensions/hash/access.rb***
|
77
|
+
1. **[`#keys_at`](https://shivam091.github.io/core_extensions/Hash.html#method-i-keys_at)** - Returns the array of keys of an occurrence of given `values`.
|
78
|
+
#### ***core_extensions/hash/delete.rb***
|
79
|
+
1. **[`#slice!`](https://shivam091.github.io/core_extensions/Hash.html#method-i-slice-21)** - Alters the hash by keeping only specified `keys` and returns it.
|
80
|
+
|
81
|
+
### [NilClass](https://shivam091.github.io/core_extensions/NilClass.html)
|
82
|
+
|
83
|
+
### ***core_extensions/nil_class***
|
84
|
+
1. **[`#blank?`](https://shivam091.github.io/core_extensions/NilClass.html#method-i-blank-3F)** - Returns `true` when invoked on `nil`.
|
85
|
+
|
86
|
+
### [Object](https://shivam091.github.io/core_extensions/Object.html)
|
87
|
+
|
88
|
+
#### ***core_extensions/object.rb***
|
89
|
+
1. **[`#is_one_of?`](https://shivam091.github.io/core_extensions/Object.html#method-i-is_one_of-3F)** - Returns `true` if the receiver object is an instance of at least one of the classes specified by `args`.
|
90
|
+
2. **[`#deep_send`](https://shivam091.github.io/core_extensions/Object.html#method-i-deep_send)** - Invokes the specified methods continuously, unless encountering a `nil` value.
|
91
|
+
3. **[`#in_namespace?`](https://shivam091.github.io/core_extensions/Object.html#method-i-in_namespace-3F)** - Returns whether or not the object is in the given namespace.
|
92
|
+
|
93
|
+
### [Module](https://shivam091.github.io/core_extensions/Module.html)
|
94
|
+
|
95
|
+
#### ***core_extensions/module.rb***
|
96
|
+
1. **[`#namespaces`](https://shivam091.github.io/core_extensions/Module.html#method-i-namespaces)** - Returns an `Array` with the namespaces to the current `Module`.
|
97
|
+
|
25
98
|
## Contributing
|
26
99
|
|
27
100
|
1. Fork it
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
# -*- warn_indent: true -*-
|
5
5
|
|
6
6
|
require "bundler/setup"
|
7
|
-
require "core_extensions"
|
7
|
+
require "core_extensions/all"
|
8
8
|
|
9
9
|
# You can add fixtures and/or initialization code here to make experimenting
|
10
10
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
require "core_extensions/array"
|
6
|
+
require "core_extensions/hash"
|
7
|
+
require "core_extensions/nil_class"
|
8
|
+
|
9
|
+
require "core_extensions/object"
|
10
|
+
require "core_extensions/module"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
class Array
|
6
|
+
# Alters the array by removing first +n+ elements.
|
7
|
+
#
|
8
|
+
# If a negative number is given, raises an +ArgumentError+.
|
9
|
+
#
|
10
|
+
# ==== Attributes
|
11
|
+
#
|
12
|
+
# * +n+ - Number of elements to take from the array.
|
13
|
+
#
|
14
|
+
# ==== Examples
|
15
|
+
#
|
16
|
+
# [].take!(3) # => []
|
17
|
+
# [1, 2, 3, 4, 5].take!(3) #=> [1, 2, 3]
|
18
|
+
# [1, 2, 3, 4, 5].take!(6) #=> [1, 2, 3, 4, 5]
|
19
|
+
def take!(n)
|
20
|
+
replace(take(n))
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
class Array
|
6
|
+
# Returns an array of the duplicate elements.
|
7
|
+
#
|
8
|
+
# ==== Examples
|
9
|
+
#
|
10
|
+
# [1, 2, 3, 2, 4, 1, 5].duplicates #=> [1, 2]
|
11
|
+
# [1, 2, 3, 4, 2, 4].duplicates #=> [2, 4]
|
12
|
+
def duplicates
|
13
|
+
self.select { |element| self.count(element) > 1 }.uniq
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
class Array
|
6
|
+
# Returns whether the array contains any of the +elements+.
|
7
|
+
#
|
8
|
+
# ==== Arguments
|
9
|
+
#
|
10
|
+
# * +elements+ - +elements+ that needs to be checked in the array.
|
11
|
+
#
|
12
|
+
# ==== Examples
|
13
|
+
#
|
14
|
+
# [1, 2, 3].include_any?(1, 2) #=> true
|
15
|
+
# [1, 2, 3].include_any?(1, 4) #=> true
|
16
|
+
# [1, 2, 3].include_any?(4, 5) #=> false
|
17
|
+
def include_any?(*elements)
|
18
|
+
!(self & elements).empty?
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns whether the array contains none of the +elements+.
|
22
|
+
#
|
23
|
+
# ==== Arguments
|
24
|
+
#
|
25
|
+
# * +elements+ - +elements+ that needs to be checked in the array.
|
26
|
+
#
|
27
|
+
# ==== Examples
|
28
|
+
#
|
29
|
+
# [1, 2, 3].include_none?(1, 2) #=> false
|
30
|
+
# [1, 2, 3].include_none?(1, 4) #=> false
|
31
|
+
# [1, 2, 3].include_none?(4, 5) #=> true
|
32
|
+
def include_none?(*elements)
|
33
|
+
(self & elements).empty?
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns whether the array contains all of the +elements+.
|
37
|
+
#
|
38
|
+
# ==== Arguments
|
39
|
+
#
|
40
|
+
# * +elements+ - +elements+ that needs to be checked in the array.
|
41
|
+
#
|
42
|
+
# ==== Examples
|
43
|
+
#
|
44
|
+
# [1, 2, 3].include_all?(1, 2) #=> true
|
45
|
+
# [1, 2, 3].include_all?(1, 4) #=> false
|
46
|
+
# [1, 2, 3].include_all?(4, 5) #=> false
|
47
|
+
def include_all?(*elements)
|
48
|
+
(elements - self).empty?
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns whether the array has a value at the specified +index+.
|
52
|
+
#
|
53
|
+
# ==== Arguments
|
54
|
+
#
|
55
|
+
# * +index+ - +index+ that needs to be checked in the array.
|
56
|
+
#
|
57
|
+
# ==== Examples
|
58
|
+
#
|
59
|
+
# [1, 2, 3].includes_index?(-4) #=> false
|
60
|
+
# [1, 2, 3].includes_index?(-3) #=> true
|
61
|
+
# [1, 2, 3].includes_index?(1) #=> true
|
62
|
+
# [1, 2, 3].includes_index?(2) #=> true
|
63
|
+
# [1, 2, 3].includes_index?(3) #=> false
|
64
|
+
def includes_index?(index)
|
65
|
+
(-self.length...self.length).cover?(index)
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
class Array
|
6
|
+
# Rounds each element of the array up to specified +precision+.
|
7
|
+
#
|
8
|
+
# If +precision+ is +zero+, array elements will be rounded to integers.
|
9
|
+
#
|
10
|
+
# ==== Attributes
|
11
|
+
#
|
12
|
+
# * +precision+ - Returns float rounded to the nearest value with a precision of +precision+.
|
13
|
+
#
|
14
|
+
# ==== Examples
|
15
|
+
#
|
16
|
+
# [1.342, 2.876, 3.546, 5.623, 5.245].round #=> [1.34, 2.88, 3.55, 5.62, 5.25]
|
17
|
+
# [1.342, 2.876, 3.546, 5.623, 5.245].round(1) #=> [1.3, 2.9, 3.5, 5.6, 5.2]
|
18
|
+
# [1.342, 2.876, 3.546, 5.623, 5.245].round(0) #=> [1, 3, 4, 6, 5]
|
19
|
+
def round(precision = 2)
|
20
|
+
map { |element| element.round(precision) }
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns the mean of the array of +Numeric+.
|
24
|
+
#
|
25
|
+
# ==== Examples
|
26
|
+
#
|
27
|
+
# [1, 2, 3, 4, 5].mean #=> 3.0
|
28
|
+
# [1.0, 2.0, 3.0].mean #=> 2.0
|
29
|
+
def mean
|
30
|
+
sum.to_f / size
|
31
|
+
end
|
32
|
+
alias_method :average, :mean
|
33
|
+
|
34
|
+
# Returns the variance of the array of +Numeric+.
|
35
|
+
#
|
36
|
+
# ==== Examples
|
37
|
+
#
|
38
|
+
# [1, 2, 3, 4, 5].variance #=> 2.0
|
39
|
+
# [1.0, 2.0, 3.0].variance #=> 0.6666666666666666
|
40
|
+
def variance
|
41
|
+
return 0 if empty?
|
42
|
+
|
43
|
+
map { |element| (element - mean) ** 2 }.sum / length
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns the standard deviation of the array of +Numeric+.
|
47
|
+
#
|
48
|
+
# ==== Examples
|
49
|
+
#
|
50
|
+
# [1, 2, 3, 4, 5].stddev #=> 1.4142135623730951
|
51
|
+
# [1.0, 2.0, 3.0].stddev #=> 0.816496580927726
|
52
|
+
def stddev
|
53
|
+
Math.sqrt(variance)
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
require "core_extensions/array/delete"
|
6
|
+
require "core_extensions/array/duplicates"
|
7
|
+
require "core_extensions/array/inclusion"
|
8
|
+
require "core_extensions/array/math"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
class Hash
|
6
|
+
# Returns the array of keys of an occurrence of given +values+.
|
7
|
+
# If the value is not found, returns empty array.
|
8
|
+
#
|
9
|
+
# ==== Arguments
|
10
|
+
#
|
11
|
+
# * +values+ - +values+ for which keys are to be retrieved.
|
12
|
+
#
|
13
|
+
# ==== Examples
|
14
|
+
#
|
15
|
+
# {a: 1, b: 2}.keys_at(1, 2) #=> [:a, :b]
|
16
|
+
# {a: 1, b: 2}.keys_at(1, 3) #=> [:a]
|
17
|
+
# {a: 1, b: 2}.keys_at(3) #=> []
|
18
|
+
def keys_at(*values)
|
19
|
+
select { |_, value| values.include?(value) }.keys
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
class Hash
|
6
|
+
# Alters the hash by keeping only specified `keys` and returns it.
|
7
|
+
# If the key is not present, returns empty `Hash`.
|
8
|
+
#
|
9
|
+
# ==== Arguments
|
10
|
+
#
|
11
|
+
# * +keys+ - +keys+ to be kept in the original `Hash`.
|
12
|
+
#
|
13
|
+
# ==== Examples
|
14
|
+
#
|
15
|
+
# {name: 'John', age: 30, occupation: 'Engineer', gender: 'Male'}.slice!(:name, :age) #=> {name: 'John', age: 30}
|
16
|
+
# {name: 'John', age: 30, occupation: 'Engineer', gender: 'Male'}.slice!(:address) #=> {}
|
17
|
+
def slice!(*keys)
|
18
|
+
keep_if { |key, _| keys.include?(key) }
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
class Hash
|
6
|
+
# Returns whether the hash contains all of the specified +keys+.
|
7
|
+
# This method is similar to Hash#key?, but it accepts several keys.
|
8
|
+
#
|
9
|
+
# ==== Arguments
|
10
|
+
#
|
11
|
+
# * +keys+ - +keys+ that needs to be checked in the hash.
|
12
|
+
#
|
13
|
+
# ==== Examples
|
14
|
+
#
|
15
|
+
# {a: 1, b: 2}.keys?(:a, :b) #=> true
|
16
|
+
# {a: 1, b: 2}.keys?(:a, :c) #=> false
|
17
|
+
def keys?(*keys)
|
18
|
+
keys.all? { |key| self.key?(key) }
|
19
|
+
end
|
20
|
+
alias_method :has_keys?, :keys?
|
21
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
class Module
|
6
|
+
# Returns an +Array+ with the namespaces to the current Module.
|
7
|
+
#
|
8
|
+
# ==== Examples
|
9
|
+
#
|
10
|
+
# Aaa::Bbb::Ccc::Ddd.namespaces #=> ["Aaa", "Bbb", "Ccc", "Ddd"]
|
11
|
+
def namespaces
|
12
|
+
name.to_s.split("::")
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
class Object
|
6
|
+
# Returns +true+ if the receiver object is an instance of at least one of the classes specified by +args+.
|
7
|
+
# This method is similar to {Object#is_a?}[https://www.rubydoc.info/stdlib/core/Object#is_a%3F-instance_method], but it accepts multiple arguments.
|
8
|
+
#
|
9
|
+
# ==== Arguments
|
10
|
+
#
|
11
|
+
# * +args+ - The classes to check against. One or more classes can be passed. Each argument represents a class that the object will be checked against.
|
12
|
+
#
|
13
|
+
# ==== Examples
|
14
|
+
#
|
15
|
+
# 1.is_one_of?(Numeric, TrueClass) #=> true
|
16
|
+
# true.is_one_of?(Numeric, TrueClass) #=> true
|
17
|
+
# false.is_one_of?(Numeric, TrueClass) #=> false
|
18
|
+
#
|
19
|
+
# ==== Returns
|
20
|
+
# (Boolean)
|
21
|
+
def is_one_of?(*args)
|
22
|
+
args.any? { |arg| is_a?(arg) }
|
23
|
+
end
|
24
|
+
|
25
|
+
# Invokes the specified methods continuously, unless encountering a +nil+ value.
|
26
|
+
#
|
27
|
+
# ==== Examples
|
28
|
+
#
|
29
|
+
# 10.deep_send("to_s.length") # => 2
|
30
|
+
# 10.deep_send("to_s", "length") # => 2
|
31
|
+
# 10.deep_send(:to_s, :length) # => 2
|
32
|
+
# 10.deep_send(["to_s", "length"]) # => 2
|
33
|
+
# [].deep_send("first.length") # => nil
|
34
|
+
def deep_send(*args)
|
35
|
+
args = args.first.dup if args.length == 1 && args.first.kind_of?(Array)
|
36
|
+
args = args.shift.to_s.strip.split('.') + args
|
37
|
+
|
38
|
+
arg = args.shift
|
39
|
+
raise ArgumentError if arg.nil?
|
40
|
+
|
41
|
+
result = send(arg)
|
42
|
+
return nil if result.nil?
|
43
|
+
return result if args.empty?
|
44
|
+
|
45
|
+
result.deep_send(args)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns whether or not the object is in the given namespace.
|
49
|
+
#
|
50
|
+
# ==== Arguments
|
51
|
+
#
|
52
|
+
# * +val+ - The object to be checked in the namespace.
|
53
|
+
#
|
54
|
+
# ==== Examples
|
55
|
+
#
|
56
|
+
# Aaa::Bbb::Ccc::Ddd.in_namespace?(Aaa::Bbb) #=> true
|
57
|
+
# Aaa::Bbb::Ccc::Ddd.new.in_namespace?(Aaa::Bbb) #=> true
|
58
|
+
# Aaa::Bbb::Ccc::Eee.in_namespace?("Aaa::Bbb") #=> true
|
59
|
+
# Aaa::Bbb::Ccc::Eee.in_namespace?(Aaa::Bbb::Ccc::Ddd) #=> false
|
60
|
+
def in_namespace?(val)
|
61
|
+
namespaces = val.to_s.split("::")
|
62
|
+
namespaces == (kind_of?(Module) ? namespaces : self.class.namespaces)[0, namespaces.length]
|
63
|
+
end
|
64
|
+
end
|
data/rb_core_extensions.gemspec
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
2
4
|
|
3
5
|
require_relative "lib/core_extensions/version"
|
4
6
|
|
@@ -31,7 +33,7 @@ Gem::Specification.new do |spec|
|
|
31
33
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
34
|
spec.require_paths = ["lib"]
|
33
35
|
|
34
|
-
spec.add_runtime_dependency "activesupport"
|
36
|
+
spec.add_runtime_dependency "activesupport", "~> 7.0"
|
35
37
|
|
36
|
-
spec.add_development_dependency "simplecov", ">= 0.21.2"
|
38
|
+
spec.add_development_dependency "simplecov", "~> 0.21", ">= 0.21.2"
|
37
39
|
end
|
metadata
CHANGED
@@ -1,33 +1,36 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rb_core_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Harshal LADHE
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-07-
|
11
|
+
date: 2023-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
19
|
+
version: '7.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
26
|
+
version: '7.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: simplecov
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.21'
|
31
34
|
- - ">="
|
32
35
|
- !ruby/object:Gem::Version
|
33
36
|
version: 0.21.2
|
@@ -35,6 +38,9 @@ dependencies:
|
|
35
38
|
prerelease: false
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0.21'
|
38
44
|
- - ">="
|
39
45
|
- !ruby/object:Gem::Version
|
40
46
|
version: 0.21.2
|
@@ -58,6 +64,19 @@ files:
|
|
58
64
|
- bin/console
|
59
65
|
- bin/setup
|
60
66
|
- lib/core_extensions.rb
|
67
|
+
- lib/core_extensions/all.rb
|
68
|
+
- lib/core_extensions/array.rb
|
69
|
+
- lib/core_extensions/array/delete.rb
|
70
|
+
- lib/core_extensions/array/duplicates.rb
|
71
|
+
- lib/core_extensions/array/inclusion.rb
|
72
|
+
- lib/core_extensions/array/math.rb
|
73
|
+
- lib/core_extensions/hash.rb
|
74
|
+
- lib/core_extensions/hash/access.rb
|
75
|
+
- lib/core_extensions/hash/delete.rb
|
76
|
+
- lib/core_extensions/hash/inclusion.rb
|
77
|
+
- lib/core_extensions/module.rb
|
78
|
+
- lib/core_extensions/nil_class.rb
|
79
|
+
- lib/core_extensions/object.rb
|
61
80
|
- lib/core_extensions/version.rb
|
62
81
|
- rb_core_extensions.gemspec
|
63
82
|
homepage: https://github.com/shivam091/core_extensions
|