rb_core_extensions 0.1.0 → 0.1.1
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/Gemfile +3 -1
- data/Gemfile.lock +3 -3
- data/README.md +47 -0
- data/Rakefile +3 -1
- data/lib/core_extensions/all.rb +5 -0
- data/lib/core_extensions/array/delete.rb +22 -0
- data/lib/core_extensions/array/duplicates.rb +16 -0
- data/lib/core_extensions/array/inclusion.rb +70 -0
- data/lib/core_extensions/array/math.rb +54 -0
- data/lib/core_extensions/array.rb +8 -0
- data/lib/core_extensions/version.rb +2 -2
- data/rb_core_extensions.gemspec +5 -3
- metadata +17 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8debb12bba2cbb5357f52feaaf7c9bb9eb3a2c54bdca4e7f9438857e985dc4fb
|
4
|
+
data.tar.gz: 95bfeab24e32c5bbd5e60e3fb278282754efb7f7a080c4be765cd647d8ca778f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ba7dfc16cfa492cacbfc41df35c59f6f00610ff5f28ff6247540eefab0bf8362bc86ff4bff340e39db67d75990ea0e31823a5d9efd56aa147c6b6ce866b22e2
|
7
|
+
data.tar.gz: 4e42b2b57b2600654c8accd9f9349fe5ce575f9e2a82b5013012b820500df59253901b2b8dfa0219a11c499d09b3035dc50e99e77c074be4e6e3938d32319b66
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
## [0.1.1](https://github.com/shivam091/core_extensions/compare/main...0.1.1) - 2023-07-05
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added core extension methods `take!`, `round`, `duplicates`, `include_all?`, `include_any?`, `include_none?`, `includes_index?`, `mean`, `stddev`, and `variance` in `Array`
|
6
|
+
|
1
7
|
## [0.1.0] - 2023-07-05
|
2
8
|
|
3
9
|
- 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.1)
|
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
|
+
[![Ruby](https://github.com/shivam091/core_extensions/actions/workflows/main.yml/badge.svg)](https://github.com/shivam091/rb_core_extensions/actions/workflows/main.yml)
|
6
|
+
[![Gem Version](https://badge.fury.io/rb/rb_core_extensions.svg)](https://badge.fury.io/rb/rb_core_extensions)
|
7
|
+
[![Gem Downloads](https://img.shields.io/gem/dt/rb_core_extensions.svg)](http://rubygems.org/gems/rb_core_extensions)
|
8
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/592b82a5336ee551a8f1/test_coverage)](https://codeclimate.com/github/shivam091/core_extensions/test_coverage)
|
9
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/592b82a5336ee551a8f1/maintainability)](https://codeclimate.com/github/shivam091/core_extensions/maintainability)
|
10
|
+
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](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,46 @@ 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
|
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
|
+
|
25
72
|
## Contributing
|
26
73
|
|
27
74
|
1. Fork it
|
data/Rakefile
CHANGED
@@ -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,16 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
class Array
|
6
|
+
#
|
7
|
+
# Returns an array of the duplicate elements.
|
8
|
+
#
|
9
|
+
# ==== Examples
|
10
|
+
#
|
11
|
+
# [1, 2, 3, 2, 4, 1, 5].duplicates #=> [1, 2]
|
12
|
+
# [1, 2, 3, 4, 2, 4].duplicates #=> [2, 4]
|
13
|
+
def duplicates
|
14
|
+
self.select { |element| self.count(element) > 1 }.uniq
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,70 @@
|
|
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
|
+
elements = elements.first if elements.length == 1 && elements.first.kind_of?(Array)
|
19
|
+
!(self & elements).empty?
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns whether the array contains none of the +elements+.
|
23
|
+
#
|
24
|
+
# ==== Arguments
|
25
|
+
#
|
26
|
+
# * +elements+ - +elements+ that needs to be checked in the array.
|
27
|
+
#
|
28
|
+
# ==== Examples
|
29
|
+
#
|
30
|
+
# [1, 2, 3].include_none?(1, 2) #=> false
|
31
|
+
# [1, 2, 3].include_none?(1, 4) #=> false
|
32
|
+
# [1, 2, 3].include_none?(4, 5) #=> true
|
33
|
+
def include_none?(*elements)
|
34
|
+
elements = elements.first if elements.length == 1 && elements.first.kind_of?(Array)
|
35
|
+
(self & elements).empty?
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns whether the array contains all of the +elements+.
|
39
|
+
#
|
40
|
+
# ==== Arguments
|
41
|
+
#
|
42
|
+
# * +elements+ - +elements+ that needs to be checked in the array.
|
43
|
+
#
|
44
|
+
# ==== Examples
|
45
|
+
#
|
46
|
+
# [1, 2, 3].include_all?(1, 2) #=> true
|
47
|
+
# [1, 2, 3].include_all?(1, 4) #=> false
|
48
|
+
# [1, 2, 3].include_all?(4, 5) #=> false
|
49
|
+
def include_all?(*elements)
|
50
|
+
elements = elements.first if elements.length == 1 && elements.first.kind_of?(Array)
|
51
|
+
(elements - self).empty?
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns whether the array has a value at the specified +index+.
|
55
|
+
#
|
56
|
+
# ==== Arguments
|
57
|
+
#
|
58
|
+
# * +index+ - +index+ that needs to be checked in the array.
|
59
|
+
#
|
60
|
+
# ==== Examples
|
61
|
+
#
|
62
|
+
# [1, 2, 3].includes_index?(-4) #=> false
|
63
|
+
# [1, 2, 3].includes_index?(-3) #=> true
|
64
|
+
# [1, 2, 3].includes_index?(1) #=> true
|
65
|
+
# [1, 2, 3].includes_index?(2) #=> true
|
66
|
+
# [1, 2, 3].includes_index?(3) #=> false
|
67
|
+
def includes_index?(index)
|
68
|
+
(-self.length...self.length).cover?(index)
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,54 @@
|
|
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 / length
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns the variance of the array of +Numeric+.
|
34
|
+
#
|
35
|
+
# ==== Examples
|
36
|
+
#
|
37
|
+
# [1, 2, 3, 4, 5].variance #=> 2.0
|
38
|
+
# [1.0, 2.0, 3.0].variance #=> 0.6666666666666666
|
39
|
+
def variance
|
40
|
+
return 0 if empty?
|
41
|
+
|
42
|
+
map { |element| (element - mean) ** 2 }.sum / length
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns the standard deviation of the array of +Numeric+.
|
46
|
+
#
|
47
|
+
# ==== Examples
|
48
|
+
#
|
49
|
+
# [1, 2, 3, 4, 5].stddev #=> 1.4142135623730951
|
50
|
+
# [1.0, 2.0, 3.0].stddev #=> 0.816496580927726
|
51
|
+
def stddev
|
52
|
+
Math.sqrt(variance)
|
53
|
+
end
|
54
|
+
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"
|
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,7 +1,7 @@
|
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Harshal LADHE
|
@@ -14,20 +14,23 @@ dependencies:
|
|
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,12 @@ 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
|
61
73
|
- lib/core_extensions/version.rb
|
62
74
|
- rb_core_extensions.gemspec
|
63
75
|
homepage: https://github.com/shivam091/core_extensions
|