rb_core_extensions 0.1.1 → 0.1.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.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +1 -1
- data/CHANGELOG.md +22 -1
- data/Gemfile.lock +1 -1
- data/README.md +62 -16
- data/bin/console +1 -1
- data/lib/core_extensions/all.rb +8 -0
- data/lib/core_extensions/array/delete.rb +3 -3
- data/lib/core_extensions/array/duplicates.rb +2 -3
- data/lib/core_extensions/array/inclusion.rb +11 -14
- data/lib/core_extensions/array/math.rb +11 -10
- data/lib/core_extensions/class.rb +40 -0
- data/lib/core_extensions/false_class.rb +23 -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 +23 -0
- data/lib/core_extensions/object.rb +64 -0
- data/lib/core_extensions/true_class.rb +23 -0
- data/lib/core_extensions/version.rb +1 -1
- metadata +12 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eaab7679a1e5cedcdfd17b5abaed34ac040d6d5f0bc7ab91e8e3d52695d78720
|
4
|
+
data.tar.gz: e61962df11083b76b1198b1762854142dd9e160d8e127fbdc6f11021d27f1d13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 889d4bb5b68918c8feaf3bf3ac4cac823f78b2899af9a0f3a7afe5f9d63fbef3c357e1da85c31bf07e893172ccaf540d70f9594ec756b0c82361a7c504f17b43
|
7
|
+
data.tar.gz: 3712ca130c9031070b2ec4d01edea3f88e8e991db450e6c3d229e39eeab21df2f34363b9a7464d66ee88970752f2ecfca4352d0d9a645954640f610b7e3ebd17
|
data/.github/workflows/main.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,25 @@
|
|
1
|
-
## [0.1.
|
1
|
+
## [0.1.3](https://github.com/shivam091/core_extensions/compare/v0.1.2...v0.1.3) - 2023-07-07
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added core extension methods `hierarchy`, `lineage`, and `leaf_subclasses` in `Class`
|
6
|
+
- Added extensions `to_i` and `to_b` in `TrueClass` and `FalseClass`
|
7
|
+
- Added `to_b` core ext. in `NilClass`
|
8
|
+
|
9
|
+
## [0.1.2](https://github.com/shivam091/core_extensions/compare/v0.1.1...v0.1.2) - 2023-07-06
|
10
|
+
|
11
|
+
### What's new
|
12
|
+
|
13
|
+
- Added core extension methods `keys?`, `slice!`, and `keys_at` in `Hash`
|
14
|
+
- Added core extension method `namespaces` in `Module`
|
15
|
+
- Added core extension method `blank?` in `NilClass`
|
16
|
+
- Added core extension methods `is_one_of?`, `deep_send`, and `in_namespace?` in `Object`
|
17
|
+
|
18
|
+
### What's changed
|
19
|
+
|
20
|
+
- Bumped version of `paambaati/codeclimate-action` workflow action to `5.0.0`
|
21
|
+
|
22
|
+
## [0.1.1](https://github.com/shivam091/core_extensions/compare/v0.1.0...v0.1.1) - 2023-07-05
|
2
23
|
|
3
24
|
### What's new
|
4
25
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -47,27 +47,73 @@ require "core_extensions/date"
|
|
47
47
|
|
48
48
|
## Extensions Provided
|
49
49
|
|
50
|
-
Below are the extension methods
|
50
|
+
Below are the extension methods provided by `rb_core_extensions`. Please refer [documentation](https://shivam091.github.io/core_extensions) for examples of how to use these methods.
|
51
51
|
|
52
|
-
### Array
|
52
|
+
### [Array](https://shivam091.github.io/core_extensions/Array.html)
|
53
53
|
|
54
|
-
|
55
|
-
|
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
56
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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
62
|
|
63
|
-
|
64
|
-
|
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
65
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
+
### [TrueClass](https://shivam091.github.io/core_extensions/TrueClass.html)
|
82
|
+
|
83
|
+
#### ***core_extensions/true_class.rb***
|
84
|
+
1. **[`#to_i`](https://shivam091.github.io/core_extensions/TrueClass.html#method-i-to_i)** - Returns `1` if invoked on `TrueClass` instance.
|
85
|
+
2. **[`#to_b`](https://shivam091.github.io/core_extensions/TrueClass.html#method-i-to_b)** - Returns `true` if invoked on `TrueClass` instance.
|
86
|
+
|
87
|
+
### [FalseClass](https://shivam091.github.io/core_extensions/FalseClass.html)
|
88
|
+
|
89
|
+
#### ***core_extensions/false_class.rb***
|
90
|
+
1. **[`#to_i`](https://shivam091.github.io/core_extensions/FalseClass.html#method-i-to_i)** - Returns `0` if invoked on `FalseClass` instance.
|
91
|
+
2. **[`#to_b`](https://shivam091.github.io/core_extensions/FalseClass.html#method-i-to_b)** - Returns `false` if invoked on `FalseClass` instance.
|
92
|
+
|
93
|
+
### [NilClass](https://shivam091.github.io/core_extensions/NilClass.html)
|
94
|
+
|
95
|
+
#### ***core_extensions/nil_class.rb***
|
96
|
+
1. **[`#blank?`](https://shivam091.github.io/core_extensions/NilClass.html#method-i-blank-3F)** - Returns `true` if invoked on `NilClass` instance.
|
97
|
+
2. **[`#to_b`](https://shivam091.github.io/core_extensions/NilClass.html#method-i-to_b)** - Returns `false` if invoked on `NilClass` instance.
|
98
|
+
|
99
|
+
### [Object](https://shivam091.github.io/core_extensions/Object.html)
|
100
|
+
|
101
|
+
#### ***core_extensions/object.rb***
|
102
|
+
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`.
|
103
|
+
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.
|
104
|
+
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.
|
105
|
+
|
106
|
+
### [Module](https://shivam091.github.io/core_extensions/Module.html)
|
107
|
+
|
108
|
+
#### ***core_extensions/module.rb***
|
109
|
+
1. **[`#namespaces`](https://shivam091.github.io/core_extensions/Module.html#method-i-namespaces)** - Returns an `Array` with the namespaces to the current `Module`.
|
110
|
+
|
111
|
+
### [Class](https://shivam091.github.io/core_extensions/Class.html)
|
112
|
+
|
113
|
+
#### ***core_extensions/class.rb***
|
114
|
+
1. **[`#hierarchy`](https://shivam091.github.io/core_extensions/Class.html#method-i-hierarchy)** - Returns a tree-like `Hash` structure of all descendants.
|
115
|
+
2. **[`#lineage`](https://shivam091.github.io/core_extensions/Class.html#method-i-lineage)** - Returns an `Array` of all superclasses.
|
116
|
+
3. **[`#leaf_subclasses`](https://shivam091.github.io/core_extensions/Class.html#method-i-leaf_subclasses)** - Returns an `Array` of all descendants which have no subclasses.
|
71
117
|
|
72
118
|
## Contributing
|
73
119
|
|
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.
|
data/lib/core_extensions/all.rb
CHANGED
@@ -3,3 +3,11 @@
|
|
3
3
|
# -*- warn_indent: true -*-
|
4
4
|
|
5
5
|
require "core_extensions/array"
|
6
|
+
require "core_extensions/hash"
|
7
|
+
require "core_extensions/true_class"
|
8
|
+
require "core_extensions/false_class"
|
9
|
+
require "core_extensions/nil_class"
|
10
|
+
|
11
|
+
require "core_extensions/class"
|
12
|
+
require "core_extensions/module"
|
13
|
+
require "core_extensions/object"
|
@@ -13,9 +13,9 @@ class Array
|
|
13
13
|
#
|
14
14
|
# ==== Examples
|
15
15
|
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
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
19
|
def take!(n)
|
20
20
|
replace(take(n))
|
21
21
|
end
|
@@ -3,13 +3,12 @@
|
|
3
3
|
# -*- warn_indent: true -*-
|
4
4
|
|
5
5
|
class Array
|
6
|
-
#
|
7
6
|
# Returns an array of the duplicate elements.
|
8
7
|
#
|
9
8
|
# ==== Examples
|
10
9
|
#
|
11
|
-
#
|
12
|
-
#
|
10
|
+
# [1, 2, 3, 2, 4, 1, 5].duplicates #=> [1, 2]
|
11
|
+
# [1, 2, 3, 4, 2, 4].duplicates #=> [2, 4]
|
13
12
|
def duplicates
|
14
13
|
self.select { |element| self.count(element) > 1 }.uniq
|
15
14
|
end
|
@@ -11,11 +11,10 @@ class Array
|
|
11
11
|
#
|
12
12
|
# ==== Examples
|
13
13
|
#
|
14
|
-
# [1, 2, 3].include_any?(1, 2)
|
15
|
-
# [1, 2, 3].include_any?(1, 4)
|
16
|
-
# [1, 2, 3].include_any?(4, 5)
|
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
17
|
def include_any?(*elements)
|
18
|
-
elements = elements.first if elements.length == 1 && elements.first.kind_of?(Array)
|
19
18
|
!(self & elements).empty?
|
20
19
|
end
|
21
20
|
|
@@ -27,11 +26,10 @@ class Array
|
|
27
26
|
#
|
28
27
|
# ==== Examples
|
29
28
|
#
|
30
|
-
# [1, 2, 3].include_none?(1, 2)
|
31
|
-
# [1, 2, 3].include_none?(1, 4)
|
32
|
-
# [1, 2, 3].include_none?(4, 5)
|
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
|
33
32
|
def include_none?(*elements)
|
34
|
-
elements = elements.first if elements.length == 1 && elements.first.kind_of?(Array)
|
35
33
|
(self & elements).empty?
|
36
34
|
end
|
37
35
|
|
@@ -43,11 +41,10 @@ class Array
|
|
43
41
|
#
|
44
42
|
# ==== Examples
|
45
43
|
#
|
46
|
-
# [1, 2, 3].include_all?(1, 2)
|
47
|
-
# [1, 2, 3].include_all?(1, 4)
|
48
|
-
# [1, 2, 3].include_all?(4, 5)
|
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
|
49
47
|
def include_all?(*elements)
|
50
|
-
elements = elements.first if elements.length == 1 && elements.first.kind_of?(Array)
|
51
48
|
(elements - self).empty?
|
52
49
|
end
|
53
50
|
|
@@ -59,8 +56,8 @@ class Array
|
|
59
56
|
#
|
60
57
|
# ==== Examples
|
61
58
|
#
|
62
|
-
# [1, 2, 3].includes_index?(-4)
|
63
|
-
# [1, 2, 3].includes_index?(-3)
|
59
|
+
# [1, 2, 3].includes_index?(-4) #=> false
|
60
|
+
# [1, 2, 3].includes_index?(-3) #=> true
|
64
61
|
# [1, 2, 3].includes_index?(1) #=> true
|
65
62
|
# [1, 2, 3].includes_index?(2) #=> true
|
66
63
|
# [1, 2, 3].includes_index?(3) #=> false
|
@@ -13,9 +13,9 @@ class Array
|
|
13
13
|
#
|
14
14
|
# ==== Examples
|
15
15
|
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
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
19
|
def round(precision = 2)
|
20
20
|
map { |element| element.round(precision) }
|
21
21
|
end
|
@@ -24,18 +24,19 @@ class Array
|
|
24
24
|
#
|
25
25
|
# ==== Examples
|
26
26
|
#
|
27
|
-
# [1, 2, 3, 4, 5].mean
|
28
|
-
# [1.0, 2.0, 3.0].mean
|
27
|
+
# [1, 2, 3, 4, 5].mean #=> 3.0
|
28
|
+
# [1.0, 2.0, 3.0].mean #=> 2.0
|
29
29
|
def mean
|
30
|
-
sum.to_f /
|
30
|
+
sum.to_f / size
|
31
31
|
end
|
32
|
+
alias_method :average, :mean
|
32
33
|
|
33
34
|
# Returns the variance of the array of +Numeric+.
|
34
35
|
#
|
35
36
|
# ==== Examples
|
36
37
|
#
|
37
|
-
# [1, 2, 3, 4, 5].variance
|
38
|
-
# [1.0, 2.0, 3.0].variance
|
38
|
+
# [1, 2, 3, 4, 5].variance #=> 2.0
|
39
|
+
# [1.0, 2.0, 3.0].variance #=> 0.6666666666666666
|
39
40
|
def variance
|
40
41
|
return 0 if empty?
|
41
42
|
|
@@ -46,8 +47,8 @@ class Array
|
|
46
47
|
#
|
47
48
|
# ==== Examples
|
48
49
|
#
|
49
|
-
# [1, 2, 3, 4, 5].stddev
|
50
|
-
# [1.0, 2.0, 3.0].stddev
|
50
|
+
# [1, 2, 3, 4, 5].stddev #=> 1.4142135623730951
|
51
|
+
# [1.0, 2.0, 3.0].stddev #=> 0.816496580927726
|
51
52
|
def stddev
|
52
53
|
Math.sqrt(variance)
|
53
54
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
require "active_support/core_ext/class/subclasses"
|
6
|
+
|
7
|
+
class Class
|
8
|
+
# Returns a tree-like +Hash+ structure of all descendants.
|
9
|
+
#
|
10
|
+
# ==== Examples
|
11
|
+
#
|
12
|
+
# require 'socket'
|
13
|
+
# IO.hierarchy
|
14
|
+
# #=> {BasicSocket=>{UNIXSocket=>{UNIXServer=>{}}, IPSocket=>{UDPSocket=>{}, TCPSocket=>{TCPServer=>{}}}, Socket=>{}}, File=>{}}
|
15
|
+
def hierarchy
|
16
|
+
subclasses.each_with_object({}) { |k, h| h[k] = k.hierarchy }
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns an +Array+ of all superclasses.
|
20
|
+
#
|
21
|
+
# ==== Examples
|
22
|
+
#
|
23
|
+
# require 'socket'
|
24
|
+
# TCPServer.lineage
|
25
|
+
# #=> [TCPSocket, IPSocket, BasicSocket, IO, Object, BasicObject]
|
26
|
+
def lineage
|
27
|
+
superclass.nil? ? [] : superclass.lineage.unshift(superclass)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns an +Array+ of all descendants which have no subclasses.
|
31
|
+
#
|
32
|
+
# ==== Examples
|
33
|
+
#
|
34
|
+
# require 'socket'
|
35
|
+
# BasicSocket.leaf_subclasses
|
36
|
+
# #=> [Socket, UNIXServer, UDPSocket, TCPServer]
|
37
|
+
def leaf_subclasses
|
38
|
+
descendants.select { |d| d.subclasses.empty? }
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
class FalseClass
|
6
|
+
# Returns +0+ if invoked on +FalseClass+ instance.
|
7
|
+
#
|
8
|
+
# ==== Examples
|
9
|
+
#
|
10
|
+
# false.to_i #=> 0
|
11
|
+
def to_i
|
12
|
+
0
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns +false+ if invoked on +FalseClass+ instance.
|
16
|
+
#
|
17
|
+
# ==== Examples
|
18
|
+
#
|
19
|
+
# false.to_b #=> false
|
20
|
+
def to_b
|
21
|
+
false
|
22
|
+
end
|
23
|
+
end
|
@@ -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,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
class NilClass
|
6
|
+
# Returns +true+ if invoked on `NilClass` instance.
|
7
|
+
#
|
8
|
+
# ==== Examples
|
9
|
+
#
|
10
|
+
# nil.blank? #=> true
|
11
|
+
def blank?
|
12
|
+
true
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns +false+ if invoked on +NilClass+ instance.
|
16
|
+
#
|
17
|
+
# ==== Examples
|
18
|
+
#
|
19
|
+
# nil.to_b #=> false
|
20
|
+
def to_b
|
21
|
+
false
|
22
|
+
end
|
23
|
+
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
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
class TrueClass
|
6
|
+
# Returns +1+ if invoked on +TrueClass+ instance.
|
7
|
+
#
|
8
|
+
# ==== Examples
|
9
|
+
#
|
10
|
+
# true.to_i #=> 1
|
11
|
+
def to_i
|
12
|
+
1
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns +true+ if invoked on +TrueClass+ instance.
|
16
|
+
#
|
17
|
+
# ==== Examples
|
18
|
+
#
|
19
|
+
# true.to_b #=> true
|
20
|
+
def to_b
|
21
|
+
true
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.3
|
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-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -70,6 +70,16 @@ files:
|
|
70
70
|
- lib/core_extensions/array/duplicates.rb
|
71
71
|
- lib/core_extensions/array/inclusion.rb
|
72
72
|
- lib/core_extensions/array/math.rb
|
73
|
+
- lib/core_extensions/class.rb
|
74
|
+
- lib/core_extensions/false_class.rb
|
75
|
+
- lib/core_extensions/hash.rb
|
76
|
+
- lib/core_extensions/hash/access.rb
|
77
|
+
- lib/core_extensions/hash/delete.rb
|
78
|
+
- lib/core_extensions/hash/inclusion.rb
|
79
|
+
- lib/core_extensions/module.rb
|
80
|
+
- lib/core_extensions/nil_class.rb
|
81
|
+
- lib/core_extensions/object.rb
|
82
|
+
- lib/core_extensions/true_class.rb
|
73
83
|
- lib/core_extensions/version.rb
|
74
84
|
- rb_core_extensions.gemspec
|
75
85
|
homepage: https://github.com/shivam091/core_extensions
|