PowerCore extends the Ruby Core with useful extensions.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
spec
.rspec
.travis.yml
Gemfile
Gemfile.lock
Guardfile
LICENSE
README.md
Rakefile

README.md

PowerCore

Code Climate Build Status

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!

Array

average

Calculate the mean of the elements.

[1, 2, 3, 4].average  # => 2.5

Negative index drop

Drop 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]

fetch_dig

Extracts the nested value specified by the sequence of idx objects by calling dig at each step.

[[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

head

Return the head of the array.

[1, 2, 3].head  # => 1

histogram

Build the histogram in a hash.

[2, 1, 2, 2, 3, 3].histogram  # => {1=>1, 2=>3, 3=>2}

mean

Calculate the mean of the elements.

[1, 2, 3, 4].mean  # => 2.5

median

Calculate the median of the elements.

[1, 2, 3, 4, 5].median  # => 3
[1, 2, 3, 4].median     # => 2.5

mode

Find 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]

percentile

Return 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

Negative index take

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]

tail

Return the tail of the array.

[1, 2, 3, 4].tail  # => [2, 3, 4]

transpose array of ranges

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]]

Date

now

Return the current day.

Date.now  # => #<Date: 2016-03-29 ((2457477j,0s,0n),+0s,2299161j)>

Integer

clamp

Clamp 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

degrees

Convert a number of degrees into radians.

90.degrees  # => 1.5707963267948966

negative

Negate the number.

1.negative  # => -1

ordinal

Return the ordinal of the number.

1.ordinal  # => "1st"
2.ordinal  # => "2nd"

Hash

except

Return the hash without keys specified.

{a: 1, b: nil, c: nil, d: 4}.except(:b, :d)  # => {a: 1, c: nil}

fetch_dig

Extracts the nested value specified by the sequence of idx objects by calling dig at each step.

{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

Kernel

λ

λ is lambda.

my_proc = λ { }

Object

assert

Assert an expression.

assert(1 == 2)  # => AssertError: AssertError
assert(1 == 1)  # => nil

in?

Return 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

metaclass

Return the eigenclass.

Object.new.metaclass  # => #<Class:#<Object:0x007fc6427d1058>>

not_in?

Return 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

not_nil?

Return true when an object is not nil.

nil.not_nil?  # => false
1.not_nil?    # => true

Smalltalk's conditionals

Emulate Smalltalk's conditionals.

(1 == 1).--> { true } { false }  # => true
(4 == 2).--> { true } { false }  # => false

Pipe operator

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"

Proc

>>

Proc forward composition.

multiply_2 = ->(var) { var * 2 }
sum_3      = ->(var) { var + 3 }
multiply_2_then_add_3 = multiply_2 >> sum_3
multiply_2_then_add_3.call(3)  # => 9

<<

Proc reverse composition.

multiply_2 = ->(var) { var * 2 }
sum_3      = ->(var) { var + 3 }
sum_3_then_multiply_2 = multiply_2 << sum_3
sum_3_then_multiply_2.call(3)  # => 12

String

first

Return the first chars of the string.

"abc".first     # => "a"
"abc".first(2)  # => "ab"

last

Return the last chars of the string.

"abc".last     # => "c"
"abc".last(2)  # => "bc"

to_bool

Convert string to boolean.

"true".to_bool   # => true
"false".to_bool  # => false

Who made this?

This was made by Arturo Herrero under the MIT License. Find me on Twitter @ArturoHerrero.