Refinements

Provides a collection of refinements to core Ruby objects.

Table of Contents

Features

Requirements

  1. Ruby 2.4.x.

  2. A solid understanding of Ruby refinements and lexical scope.

Setup

For a secure install, type the following from the command line (recommended):

gem cert --add <(curl --location --silent https://www.alchemists.io/gem-public.pem)
gem install refinements --trust-policy MediumSecurity

NOTE: A HighSecurity trust policy would be best but MediumSecurity enables signed gem verification while allowing the installation of unsigned dependencies since they are beyond the scope of this gem.

For an insecure install, type the following (not recommended):

gem install refinements

Add the following to your Gemfile file:

gem "refinements"

Usage

Requires

If all refinements are not desired, add the following to your Gemfile instead:

gem "refinements", require: false

…then require the specific refinement, as needed. Example:

require "refinements/arrays"
require "refinements/big_decimals"
require "refinements/hashes"
require "refinements/strings"

Using

Much like including/extending a module, you'll need modify your object(s) to use the refinement(s):

class Example
  using Refinements::Arrays
  using Refinements::BigDecimals
  using Refinements::Hashes
  using Refinements::Strings
end

Examples

The following sections demonstrate how each refinement enriches your objects with new capabilities.

String

"example".first # => "e"
"example".first 4 # => "exam"

"instant".last # => "t"
"instant".first 3 # => "ant"

" \n\t\r".blank? # => true
"example".up # => "Example"
"EXAMPLE".down # => "eXAMPLE"
"this_is_an_example".camelcase # => "ThisIsAnExample"
"ThisIsAnExample".snakecase # => "this_is_an_example"
"ThisIsAnExample".titleize # => "This Is An Example"

Big Decimal

big = BigDecimal.new "5.0E-10"
big.inspect # => "#<BigDecimal:3fd3d458fe84 0.0000000005>"

Array

example = ["An", nil, "", "Example"]
example.compress # => ["An", "Example"]
example # => ["An", nil, "", "Example"]

example = ["An", nil, "", "Example"]
example.compress! # => ["An", "Example"]
example # => ["An", "Example"]

Hash

example = {a: 1, b: 2, c: 3}
example.except :a, :b # => {c: 3}
example # => {a: 1, b: 2, c: 3}

example = {a: 1, b: 2, c: 3}
example.except! :a, :b # => {c: 3}
example # => {c: 3}

example = {"a" => 1, "b" => 2}
example.symbolize_keys # => {a: 1, b: 2}
example # => {"a" => 1, "b" => 2}

example = {"a" => 1, "b" => 2}
example.symbolize_keys! # => {a: 1, b: 2}
example # => {a: 1, b: 2}

example = {a: 1, b: 2, c: 3}
example.slice :a, :c # => {a: 1, c: 3}
example # => {a: 1, b: 2, c: 3}

example = {a: 1, b: 2, c: 3}
example.slice! :a, :c # => {a: 1, c: 3}
example # => {a: 1, c: 3}

example = {a: "A", b: {one: "One", two: "Two"}}
example.deep_merge b: {one: 1} # => {a: "A", b: {one: 1, two: "Two"}}
example # => {a: "A", b: {one: "One", two: "Two"}}

example = {a: "A", b: {one: "One", two: "Two"}}
example.deep_merge! b: {one: 1} # => {a: "A", b: {one: 1, two: "Two"}}
example # => {a: "A", b: {one: 1, two: "Two"}}

example = {a: 1, b: 2}
example.reverse_merge a: 0, c: 3 # => {a: 1, b: 2, c: 3}
example # => {a: 1, b: 2}

example = {a: 1, b: 2}
example.reverse_merge! a: 0, c: 3 # => {a: 1, b: 2, c: 3}
example # => {a: 1, b: 2, c: 3}

example = {unit: "221B", street: "Baker Street", city: "London", country: "UK"}
example.use { |unit, street| "#{unit} #{street}" } # => "221B Baker Street"

Tests

To test, run:

bundle exec rake

Versioning

Read Semantic Versioning for details. Briefly, it means:

Code of Conduct

Please note that this project is released with a CODE OF CONDUCT. By participating in this project you agree to abide by its terms.

Contributions

Read CONTRIBUTING for details.

License

Copyright © 2015 Alchemists. Read LICENSE for details.

History

Read CHANGES for details. Built with Gemsmith.

Credits

Developed by Brooke Kuhlmann at Alchemists.