PowerCore extends the Ruby Core with useful extensions.
There are Ruby gems that do something similar, such as ActiveSuppor Core Extensions or Powerpack.
In this case, this is just a collection of extensions for reference, not a Ruby gem. Who wants a new dependency in the code base? Just borrow the code that you consider useful, but be careful; most of the time I have created new methods but sometimes I have overridden the default Ruby implementation, or even worse, I have removed Ruby methods to do some tricks. Come and see!
Calculates the mean of the elements.
[1, 2, 3, 4].average # => 2.5
Drops n elements from end of the array.
[1, 2, 3, 4].drop(-2) # => [1, 2]
If you pass a positive number, it delegates to the original implementation.
[1, 2, 3, 4].drop(2) # => [3, 4]
Returns the array without the indexes specified.
[1, 2, 3, 4].except(1, 2) # => [1, 4]
Extracts the nested value specified by the sequence of indexes.
[[1, [2, 3]]].fetch_dig(0, 1, 1) # => 3
If the key can’t be found with no other arguments, it will raise an IndexError
exception.
[[1, [2, 3]]].fetch_dig(1, 2, 3) # => IndexError
If the key can’t be found and an optional code block is specified, then that will be run and its result returned.
[[1, [2, 3]]].fetch_dig(1, 2, 3) { 2 } # => 2
Returns the head of the array.
[1, 2, 3].head # => 1
Builds the histogram in a hash.
[2, 1, 2, 2, 3, 3].histogram # => {1=>1, 2=>3, 3=>2}
The initial part of the array without its last element.
[1, 2, 3].init # => [1, 2]
Calculates the mean of the elements.
[1, 2, 3, 4].mean # => 2.5
Calculates the median of the elements.
[1, 2, 3, 4, 5].median # => 3
[1, 2, 3, 4].median # => 2.5
Finds the mode value/s.
[1, 2, 3, 4].mode # => [1, 2, 3, 4]
[1, 2, 2, 4].mode # => [2]
[1, 1, 2, 4, 4].mode # => [1, 4]
Returns the percentile value for a given percentage.
[1, 2, 3, 4].percentile(49) # => 2
[1, 2, 3, 4].percentile(50) # => 3
[1, 2, 3, 4, 5].percentile(50) # => 3
Returns n elements from end of the array.
[1, 2, 3, 4].take(-2) # => [3, 4]
If you pass a positive number, it delegates to the original implementation.
[1, 2, 3, 4].take(2) # => [1, 2]
Returns the tail of the array.
[1, 2, 3, 4].tail # => [2, 3, 4]
Assumes that self is an array of ranges and transposes the rows and columns.
[(1..2), (3..4)].transpose # => [[1, 3], [2, 4]]
It also works with the original implementation, assuming an array of arrays.
[[1, 2], [3, 4]].transpose # => [[1, 3], [2, 4]]
Returns the current day.
Date.now # => #<Date: 2016-03-29 ((2457477j,0s,0n),+0s,2299161j)>
Clamps a comparable between a lower and upper bound.
1.clamp(3, 6) # => 3
5.clamp(3, 6) # => 5
8.clamp(3, 6) # => 6
1.clamp(3..6) # => 3
5.clamp(3..6) # => 5
8.clamp(3..6) # => 6
Converts a number of degrees into radians.
90.degrees # => 1.5707963267948966
Negates the number.
1.negative # => -1
Returns the ordinal of the number.
1.ordinal # => "1st"
2.ordinal # => "2nd"
Returns the hash without the keys specified.
{ a: 1, b: nil, c: nil, d: 4 }.except(:b, :d) # => {a: 1, c: nil}
Extracts the nested value specified by the sequence of keys.
{ foo: { bar: { baz: 1 } }}.fetch_dig(:foo, :bar, :baz) # => 1
If the key can’t be found with no other arguments, it will raise an KeyError
exception.
{ foo: { bar: { baz: 1 } } }.fetch_dig(:foo, :zot, :xyz) # => KeyError
If the key can’t be found and an optional code block is specified, then that will be run and its result returned.
{ foo: { bar: { baz: 1 } } }.fetch_dig(:foo, :zot, :xyz) { 2 } # => 2
Returns the first elements of the hash.
{ a: 1, b: 2, c: 3 }.first # => { a: 1 }
{ a: 1, b: 2, c: 3 }.first(2) # => { a: 1, b: 2 }
Returns the first element of the hash.
{ a: 1, b: 2, c: 3 }.head # => { a: 1 }
The initial part of the hash without its last element.
{ a: 1, b: 2, c: 3 }.init # => { a: 1, b: 2 }
The initial part of the hash without its last element.
{ a: 1, b: 2, c: 3 }.last # => { c: 3 }
The rest of the hash without its first element
{ a: 1, b: 2, c: 3 }.tail # => { b: 2, c: 3 }
λ is lambda.
my_proc = λ { }
Asserts an expression.
assert(1 == 2) # => AssertError: AssertError
assert(1 == 1) # => nil
Returns true if self is present in the given object.
1.in?([1, 2, 3]) # => true
"lo".in?("hello") # => true
:b.in?({ a: 100, b: 200 }) # => true
Returns the eigenclass.
Object.new.metaclass # => #<Class:#<Object:0x007fc6427d1058>>
Returns true if self is not present in the given object.
4.not_in?([1, 2, 3]) # => true
"mo".not_in?("hello") # => true
:c.not_in?({ a: 100, b: 200 }) # => true
Returns true when an object is not nil.
nil.not_nil? # => false
1.not_nil? # => true
Pipe operator à la Bash/Elixir.
[1, 2, 3] |
->(array) { array.first } |
->(int) { int.to_s } |
->(string) { string + "2" }
# => "12"
[1, 2, 3] | :first | :to_s | ->(s) { s + "2" } # => "12"
Returns the first object in the range.
(0..3).head # => 0
("a".."z").head # => "a"
The initial part of the range without its last element.
(0..3).init # => 0..2
("a".."z").init # => "a".."y"
The rest of the range without its first element.
(0..3).tail # => 1..3
("a".."z").tail # => "b".."z"
Returns the first characters of the string.
"abc".first # => "a"
"abc".first(2) # => "ab"
Returns the first characters of the string.
"abc".head # => "a"
The initial part of the string without its last element.
"abc".init # => "ab"
Returns the last characters of the string.
"abc".last # => "c"
"abc".last(2) # => "bc"
The rest of the string without its first element.
"abc".tail # => "bc"
Converts a string to boolean.
"true".to_bool # => true
"false".to_bool # => false
This was made by Arturo Herrero under the MIT License. Find me on Twitter @ArturoHerrero.