Rearmed Ruby
A collection of helpful methods and monkey patches for Arrays, Hash, Enumerables, Strings, Objects & Dates in Ruby. Rearmed is a collection of plugins which are driven by making life easier & coding more natural.
The difference between this library and others is that all monkey patching is performed in an opt-in way because you shouldnt be using methods you dont know about anyways.
When possible I have placed the method implementations inside the Rearmed module so if you don't like monkey patching or are working on the project with a team then you can use these methods instead. You can then skip the config and see how to use each implementation below the relevant methods documentation.
Install
Add the following line to your gemfile:
gem 'rearmed'
Usage
Setup Monkey Patches (all are opt-in)
Rearmed.enabled_patches = {
array: {
delete_first: false,
dig: false,
not_empty: false
},
date: {
now: false
},
enumerable: {
natural_sort: false,
natural_sort_by: false,
select_map: false
},
hash: {
compact: false,
dig: false,
join: false,
only: false,
to_struct: false
},
integer: {
length: false
},
object: {
bool?: false,
false?: false,
in: false,
not_nil: false,
true?: false
},
string: {
begins_with: false,
casecmp?: false,
ends_with: false,
match?: false,
starts_with: false,
to_bool: false,
valid_float: false,
valid_integer: false
}
}
Rearmed.apply_patches!
Some other argument formats the enabled_patches
option accepts are:
Rearmed.enabled_patches = :all
Rearmed.enabled_patches = nil
Rearmed.enabled_patches = {
array: true,
date: {
now: true
},
enumerable: true,
hash: false,
integer: false,
object: nil,
string: nil
}
By design, once Rearmed.apply_patches!
is called then Rearmed.enabled_patches
is no longer editable and Rearmed.apply_patches!
cannot be called again. If you try to do so, it will raise a PatchesAlreadyAppliedError
. There is no-built in way of changing the patches, if you need to do so (which you shouldn't) that is up to you to figure out.
Array Methods
array = [1,2,1,4,1]
array.delete_first(1)
puts array
array.delete_first{|x| 1 == x}
puts array
array.delete_first
puts array
array.not_empty?
items = [{foo: ['foo','bar']}, {test: 'thing'}]
items.dig(0, :foo, 1)
Enumerable Methods (Array, Hash, etc.)
items = ['1.1', '1.11', '1.2']
items.natural_sort
items.natural_sort(reverse: true)
items = [{version: "1.1"}, {version: "1.11"}, {version: "1.2"}]
items.natural_sort_by{|x| x[:version]}
items = [{version: "1.1"}, {version: nil}, {version: false}]
items.select_map{|x| x[:version]}
Date
Date.now
Hash Methods
hash.join{|k,v| "#{k}: #{v}\n"}
hash = {foo: 'foo', bar: 'bar', other: 'other'}
hash.only(:foo, :bar)
hash.only!(:foo, :bar)
hash.to_struct
items = [{foo: ['foo','bar']}, {test: 'thing'}]
items.dig(0, :foo, 1)
hash.compact
hash.compact!
Object
my_var.not_nil?
my_var.in?([1,2,3])
my_var.in?(1,2,3)
my_var.bool?
my_var.true?
my_var.false?
String
'123'.valid_integer?
'123.123'.valid_float?
'123.123'.valid_number?
'true'.to_bool
'foo'.match?(/fo/)
'foo'.starts_with?('fo')
'foo'.begins_with?('fo')
'bar'.ends_with?('ar')
'foo'.casecmp?('FOO')
'foo'.casecmp?('FOOBAR')
Contributing / Todo
If your looking to contribute here is a list of outstanding items:
- Get the
natural_sort
method to accept a block
To request or add a method, please raise an issue and we can discuss the implementation.
Credits
Created by Weston Ganger - @westonganger
For any consulting or contract work please contact me via my company website: Solid Foundation Web Development
Other Libraries in the Rearmed family of Plugins