{"id":13835635,"url":"https://github.com/westonganger/rearmed-rb","last_synced_at":"2025-07-10T10:30:47.445Z","repository":{"id":56891293,"uuid":"57971251","full_name":"westonganger/rearmed-rb","owner":"westonganger","description":"A collection of helpful methods and monkey patches for Arrays, Hash, Enumerables, Strings, Objects \u0026 Dates in Ruby","archived":true,"fork":false,"pushed_at":"2023-02-14T05:39:24.000Z","size":114,"stargazers_count":42,"open_issues_count":3,"forks_count":1,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-29T07:40:30.379Z","etag":null,"topics":["monkey-patching","rails","ruby","utility"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/westonganger.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-03T13:27:37.000Z","updated_at":"2024-06-11T05:25:49.000Z","dependencies_parsed_at":"2023-01-30T17:01:32.363Z","dependency_job_id":null,"html_url":"https://github.com/westonganger/rearmed-rb","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Frearmed-rb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Frearmed-rb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Frearmed-rb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Frearmed-rb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/westonganger","download_url":"https://codeload.github.com/westonganger/rearmed-rb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225632936,"owners_count":17499890,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["monkey-patching","rails","ruby","utility"],"created_at":"2024-08-04T14:01:07.127Z","updated_at":"2024-11-20T21:31:13.787Z","avatar_url":"https://github.com/westonganger.png","language":"Ruby","funding_links":["https://ko-fi.com/A5071NK'"],"categories":["Ruby"],"sub_categories":[],"readme":"# Rearmed Ruby\n\n\u003ca href=\"https://badge.fury.io/rb/rearmed\" target=\"_blank\"\u003e\u003cimg height=\"21\" style='border:0px;height:21px;' border='0' src=\"https://badge.fury.io/rb/rearmed.svg\" alt=\"Gem Version\"\u003e\u003c/a\u003e\n\u003ca href='https://github.com/westonganger/rearmed-rb/actions' target='_blank'\u003e\u003cimg src=\"https://github.com/westonganger/rearmed-rb/workflows/Tests/badge.svg\" style=\"max-width:100%;\" height='21' style='border:0px;height:21px;' border='0' alt=\"CI Status\"\u003e\u003c/a\u003e\n\u003ca href='https://rubygems.org/gems/rearmed' target='_blank'\u003e\u003cimg height='21' style='border:0px;height:21px;' src='https://img.shields.io/gem/dt/rearmed?color=brightgreen\u0026label=Rubygems%20Downloads' border='0' alt='RubyGems Downloads' /\u003e\u003c/a\u003e\n\u003ca href='https://ko-fi.com/A5071NK' target='_blank'\u003e\u003cimg height='22' style='border:0px;height:22px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /\u003e\u003c/a\u003e \n\nA collection of helpful methods and monkey patches for Arrays, Hash, Enumerables, Strings, Objects \u0026 Dates in Ruby. [Rearmed is a collection of plugins](https://github.com/westonganger?utf8=%E2%9C%93\u0026tab=repositories\u0026q=rearmed) which are driven by making life easier \u0026 coding more natural.\n\nThe 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. \n\nWhen 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.\n\n\n# Install\n\nAdd the following line to your gemfile:\n```ruby\ngem 'rearmed'\n```\n\n# Usage\n\n## Setup Monkey Patches (all are opt-in)\n\n```ruby\n# config/initializers/rearmed.rb\n\nRearmed.enabled_patches = {\n  array: {\n    delete_first: false,\n    dig: false,\n    not_empty: false\n  },\n  date: {\n    now: false\n  },\n  enumerable: {\n    natural_sort: false,\n    natural_sort_by: false,\n    select_map: false\n  },\n  hash: {\n    compact: false,\n    deep_set: false,\n    dig: false,\n    join: false,\n    only: false,\n    to_struct: false\n  },\n  integer: {\n    length: false\n  },\n  object: {\n    bool?: false,\n    false?: false,\n    in: false,\n    not_nil: false,\n    true?: false\n  },\n  string: {\n    begins_with: false,\n    casecmp?: false,\n    ends_with: false,\n    match?: false,\n    starts_with: false,\n    to_bool: false,\n    valid_float: false,\n    valid_integer: false\n  }\n}\n\nRearmed.apply_patches!\n```\n\nSome other argument formats the `enabled_patches` option accepts are:\n\n```ruby\n### Enable everything\nRearmed.enabled_patches = :all\n\n### Disable everything\nRearmed.enabled_patches = nil\n\n### Hash values can be boolean/nil values also\nRearmed.enabled_patches = {\n  array: true, \n  date: {\n    now: true\n  },\n  enumerable: true, \n  hash: false, \n  integer: false, \n  object: nil, \n  string: nil\n}\n```\n\nBy 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.\n\n## Array Methods\n```ruby\narray = [1,2,1,4,1]\narray.delete_first(1) # =\u003e 1\nputs array #=\u003e [2,1,4,1]\narray.delete_first{|x| 1 == x} # =\u003e 1\nputs array # =\u003e [2,4,1]\narray.delete_first # =\u003e 2\nputs array # =\u003e [4,1]\n\narray.not_empty? # =\u003e true\n\n# Only monkey patched if using Ruby 2.2.x or below as this method was added to Ruby core in 2.3.0\nitems = [{foo: ['foo','bar']}, {test: 'thing'}]\nitems.dig(0, :foo, 1) # =\u003e 'bar'\n# or without monkey patch: Rearmed.dig(items, 0, :foo, 1)\n```\n\n## Enumerable Methods (Array, Hash, etc.)\n```ruby\nitems = ['1.1', '1.11', '1.2']\nitems.natural_sort \nitems.natural_sort(reverse: true) # because natural_sort does not accept a block, accepting PR's on this\n# or without monkey patch: Rearmed.natural_sort(items) or Rearmed.natural_sort(items, reverse: true)\n\nitems = [{version: \"1.1\"}, {version: \"1.11\"}, {version: \"1.2\"}]\nitems.natural_sort_by{|x| x[:version]} \n# or without monkey patch: Rearmed.natural_sort_by(items){|x| x[:version]}\n\nitems = [{version: \"1.1\"}, {version: nil}, {version: false}]\nitems.select_map{|x| x[:version]} #=\u003e [{version: \"1.1\"}]\n# or without monkey patch: Rearmed.select_map(items){|x| x[:version]}\n```\n\n## Date\n```ruby\nDate.now\n```\n\n## Hash Methods\n```ruby\nhash.join{|k,v| \"#{k}: #{v}\\n\"}\n\nhash = {foo: 'foo', bar: 'bar', other: 'other'}\nhash.only(:foo, :bar) # =\u003e {foo: 'foo'}\n# or without monkey patch: Rearmed.hash_only(hash, :foo, :bar)\n\nhash.only!(:foo, :bar)\n\nhash.to_struct\n# or without monkey patch: Rearmed.hash_to_struct(hash)\n\n# Only monkey patched if using Ruby 2.2.x or below as this method was added to Ruby core in 2.3.0\nitems = [{foo: ['foo','bar']}, {test: 'thing'}]\nitems.dig(0, :foo, 1) # =\u003e 'bar'\n# or without monkey patch: Rearmed.dig(items, 0, :foo, 1)\n\n# Only monkey patched if using Ruby 2.3.x or below as this method was added to Ruby core in 2.4.0\nhash.compact\n# or without monkey patch: Rearmed.hash_compact(hash)\nhash.compact!\n\nhash.deep_set([:foo, :bar, :name], \"John\") # =\u003e {foo: {bar: {name: \"John\"}}}\n# or without monkey patch: Rearmed.hash_deep_set(hash, keys_array, value)\n```\n\n## Object\n```ruby\nmy_var.not_nil?\n\n# Only monkey patched if not using ActiveSupport / Rails as this method is already defined there\nmy_var.in?([1,2,3])\nmy_var.in?(1,2,3) # or with splat arguments\n\nmy_var.bool? # if is true or false boolean value\nmy_var.true? # if is true boolean value\nmy_var.false? # if is false boolean value\n```\n\n## String\n```ruby\n'123'.valid_integer?\n# or without monkey patch: Rearmed.valid_integer?('123')\n\n'123.123'.valid_float? \n# or without monkey patch: Rearmed.valid_float?('123.123')\n\n'123.123'.valid_number? \n# or without monkey patch: Rearmed.valid_number?('123.123')\n\n'true'.to_bool \n# or without monkey patch: Rearmed.to_bool('true')\n\n'foo'.match?(/fo/) #=\u003e true, this method returns a convenient boolean value instead of matchdata\n\n# alias of start_with? and end_with? to have more sensible method names\n'foo'.starts_with?('fo') # =\u003e true\n'foo'.begins_with?('fo') # =\u003e true\n'bar'.ends_with?('ar') # =\u003e true\n\n# Only monkey patched if using Ruby 2.3.x or below as this method was added to Ruby core in 2.4.0\n'foo'.casecmp?('FOO') #=\u003e true\n'foo'.casecmp?('FOOBAR') #=\u003e false\n# or without monkey patch: Rearmed.casecmp?('foo', 'FOO')\n```\n\n# Contributing / Todo\nIf your looking to contribute here is a list of outstanding items:\n\n- Get the `natural_sort` method to accept a block\n\nTo request or add a method, please raise an issue and we can discuss the implementation. \n\n\n# Credits\nCreated by Weston Ganger - [@westonganger](https://github.com/westonganger)\n\n## Other Libraries in the Rearmed family of Plugins\n- [Rearmed Rails](https://github.com/westonganger/rearmed_rails)\n- [Rearmed-JS](https://github.com/westonganger/rearmed-js)\n- [Rearmed-CSS](https://github.com/westonganger/rearmed-css)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwestonganger%2Frearmed-rb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwestonganger%2Frearmed-rb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwestonganger%2Frearmed-rb/lists"}