DEV Community

Andy Maleh
Andy Maleh

Posted on

4 3

ArrayIncludeMethods Gem Now Supports RubyMotion

The array_include_methods gem v1.5.0 now supports RubyMotion in addition to MRI CRuby, JRuby, and Opal.

Below is a reminder of what the array_include_methods gem is all about, taken straight from its README.

Happy learning!

ArrayIncludeMethods 1.5.0 - Ruby Refinement

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

Setup

With Bundler:

Include the following in Gemfile:

gem 'array_include_methods', '~> 1.5.0'
Enter fullscreen mode Exit fullscreen mode

Run:

bundle
Enter fullscreen mode Exit fullscreen mode

Without Bundler:

Run:

gem install array_include_methods -v1.5.0
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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

using ArrayIncludeMethods
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 []
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay