Git Product home page Git Product logo

array_include_methods's Introduction

ArrayIncludeMethods 1.5.1 - Ruby Refinement

Gem Version Build Status Coverage Status

Array#include_all?, Array#include_any?, Array#include_array?, Array#array_index, Array#counts, and Array#duplicates methods missing from basic Ruby Array API.

Setup

With Bundler:

Include the following in Gemfile:

gem 'array_include_methods', '~> 1.5.1'

Run:

bundle

Without Bundler:

Run:

gem install array_include_methods -v1.5.1

Usage

Add the following line to your application if you are not requiring all gems via Bundler (e.g. Bundler.require(:default)):

require 'array_include_methods'

To activate the ArrayIncludeMethods Ruby Refinement for the Array class, add the following line to every Ruby file that needs it:

using ArrayIncludeMethods

Now, you have #include_all?, #include_any?, #include_array?, #array_index, #array_diff_indices, and #array_intersection_indices methods on Array objects.

Examples

Array#include_any?(*other_array)

[1, 2, 3, 4].include_any?(2, 4, 5) # returns true
[1, 2, 3, 4].include_any?(6, 7) # returns false
[1, 2, 3, 4].include_any?() # returns true
[1, 2, 3, 4].include_any?(nil) # returns false

Array#include_all?(*other_array)

[1, 2, 3, 4].include_all?(2, 3) # returns true
[1, 2, 3, 4].include_all?(2, 4) # returns true
[1, 2, 3, 4].include_all?(4, 2) # returns true
[1, 2, 3, 4].include_all?(4, 2, same_sort: true) # returns false
[1, 2, 3, 4].include_all?(2, 4, 4) # returns true
[1, 2, 3, 4].include_all?(2, 4, 5) # returns false
[1, 2, 3, 4].include_all?() # returns true
[1, 2, 3, 4].include_all?(nil) # returns false

Array#include_array?(other_array)

[1, 2, 3, 4].include_array?([2, 3]) # returns true
[1, 2, 3, 4].include_array?([2, 4]) # returns false
[1, 2, 3, 4].include_array?([4, 2]) # returns false
[1, 2, 3, 4].include_array?([2, 4, 4]) # returns false
[1, 2, 3, 4].include_array?([2, 4, 5]) # returns false
[1, 2, 3, 4].include_array?([]) # returns true
[1, 2, 3, 4].include_array?([nil]) # returns false

Array#array_index(other_array)

Returns first array index of other_array in first_array assuming first_array.include_all?(other_array) returns true

[1, 2, 3, 4].array_index([2, 3, 4]) # returns 1
[1, 2, 3, 4].array_index([2, 3]) # returns 1
[1, 2, 3, 4].array_index([3, 4]) # returns 2
[1, 2, 3, 4].array_index([2, 4]) # returns -1
[1, 2, 3, 4].array_index([4, 2]) # returns -1
[1, 2, 3, 4].array_index([2, 4, 5]) # returns -1
[1, 2, 3, 4].array_index([]) # returns -1
[1, 2, 3, 4].array_index(nil) # returns -1

Array#array_intersection_indexes(other_array)

(alias: Array#array_intersection_indices(other_array))

Returns indexes from self array for which elements match elements in other_array assuming same sort

[1, 2, 3, 4].array_intersection_indexes([2, 3, 4]) # returns [1, 2, 3]
[1, 2, 3, 4].array_intersection_indexes([2, 3]) # returns [1, 2]
[1, 2, 3, 4].array_intersection_indexes([3, 4]) # returns [2, 3]
[1, 2, 3, 4].array_intersection_indexes([2, 4]) # returns [1, 3]
[1, 2, 3, 4].array_intersection_indexes([4, 2]) # returns [3]
[1, 2, 3, 4].array_intersection_indexes([2, 4, 5]) # returns [1, 3]
[1, 2, 3, 4].array_intersection_indexes([]) # returns []
[1, 2, 3, 4].array_intersection_indexes(nil) # returns []

Array#array_diff_indexes(other_array)

(alias: Array#array_diff_indices(other_array))

Returns indexes from self array for which elements do not match elements in other_array assuming same sort

[1, 2, 3, 4].array_diff_indexes([2, 3, 4]) # returns [0]
[1, 2, 3, 4].array_diff_indexes([2, 3]) # returns [0, 3]
[1, 2, 3, 4].array_diff_indexes([3, 4]) # returns [0, 1]
[1, 2, 3, 4].array_diff_indexes([2, 4]) # returns [0, 2]
[1, 2, 3, 4].array_diff_indexes([4, 2]) # returns [0, 1, 2]
[1, 2, 3, 4].array_diff_indexes([2, 4, 5]) # returns [0, 2]
[1, 2, 3, 4].array_diff_indexes([]) # returns [0, 1, 2, 3]
[1, 2, 3, 4].array_diff_indexes(nil) # returns [0, 1, 2, 3]

Array#counts

Returns a hash of counts of every element in the array, performed in linear time (running time of O(n)).

[1, 2, 3, 4].counts # returns {1=>1, 2=>1, 3=>1, 4=>1}
[1, :a, :a, :b, 'bee', 'see', true, true, nil, nil].counts # returns {1=>1, :a=>2, :b=>1, "bee"=>1, "see"=>1, true=>2, nil=>2}
[1, :a, :a, :b, 'bee', 'see', true, true, nil].counts # {1=>1, :a=>2, :b=>1, "bee"=>1, "see"=>1, true=>2, nil=>1}
[1, {a: 1}, :a, :a, :b, 'bee', 'see', true, true, {a: 1}, {a: 1}].counts # {1=>1, {:a=>1}=>3, :a=>2, :b=>1, "bee"=>1, "see"=>1, true=>2}
[].counts # returns {}

Array#duplicates

Returns a single occurrence of all elements that repeated in an array, performed in linear time (running time of O(n)).

[1, 2, 3, 4].duplicates # returns []
[1, :a, :a, :b, 'bee', 'see', true, true, nil, nil].duplicates # returns [:a, true, nil]
[1, :a, :a, :b, 'bee', 'see', true, true, nil].duplicates # returns [:a, true]
[1, {a: 1}, :a, :a, :b, 'bee', 'see', true, true, {a: 1}, {a: 1}].duplicates # returns [:a, true, {a: 1}]
[].duplicates # returns []

JRuby Compatibility

This gem is 100% compatible with JRuby.

Opal Compatibility

This gem degrades gracefully to monkey-patching in Opal Ruby and provides a using method shim so consumer code does not have to change if it used gems that rely on the Ruby refinement.

RubyMotion Compatibility

This gem degrades gracefully to monkey-patching in RubyMotion and provides a using method shim so consumer code does not have to change if it used gems that rely on the Ruby refinement.

TODO

TODO.md

Change Log

CHANGELOG.md

Contributing to array_include_methods

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

MIT

Copyright (c) 2020-2022 Andy Maleh. See LICENSE.txt for further details.

array_include_methods's People

Contributors

andyobtiva avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

rubycoder

array_include_methods's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.