A library of utility services and concerns.
Tools for working with array-like enumerable objects.
ARRAY_METHODS
= %i[[] count each].freeze
Expected methods that an Array-like object should implement.
OTHER_METHODS
= %i[each_key each_pair].freeze
Methods that an Array-like object should not implement.
.instance => SleepingKingStudios::Tools::Base (deprecated)
Deprecated. v1.3.0 Use Toolbelt.instance instead.
#initialize(toolbelt: nil) => Base
#array?(obj) => Boolean
Returns true if the object is or appears to be an Array.
This method checks for the method signatures of the object. An Array-like method will define all of the the #[], #count, and #each methods, and neither of the #each_key or #each_pair methods.
ArrayTools.array?(nil)
#=> false
ArrayTools.array?([])
#=> true
ArrayTools.array?({})
#=> false#bisect(ary, &) => Array<Array<Object>> (deprecated)
Deprecated. v1.3.0 Use Enumerable#partition instead.
Partitions the array into matching and non-matching items.
Separates the array into two arrays, the first containing all items in the original array that matches the provided block, and the second containing all items in the original array that do not match the provided block.
selected, rejected = ArrayTools.bisect([*0...10]) { |item| item.even? }
selected
#=> [0, 2, 4, 6, 8]
rejected
#=> [1, 3, 5, 7, 9]#count_values(ary) => Hash{Object => Integer} (deprecated)#count_values(ary, &block) => Hash{Object => Integer} (deprecated)Also known as:
tally
Deprecated. v1.3.0 Use Enumerable#tally instead.
Counts the number of times each item or result appears in the object.
#count_values(ary) => Hash{Object => Integer}
Counts the number of times each value appears in the enumerable object.
ArrayTools.count_values([1, 1, 1, 2, 2, 3])
#=> { 1 => 3, 2 => 2, 3 => 1 }#count_values(ary, &block) => Hash{Object => Integer}
Calls the block and counts the number of times each result appears.
ArrayTools.count_values([1, 1, 1, 2, 2, 3]) { |i| i ** 2 }
#=> { 1 => 3, 4 => 2, 9 => 1 }#deep_dup(ary) => Array
Creates a deep copy of the array and its contents.
Iterates over the array and returns a new Array with deep copies of each array item.
ary = ['one', 'two', 'three']
cpy = ArrayTools.deep_dup ary
cpy << 'four'
#=> ['one', 'two', 'three', 'four']
ary
#=> ['one', 'two', 'three']
cpy.first.sub!(/on/, 'vu')
cpy
#=> ['vun', 'two', 'three', 'four']
ary
#=> ['one', 'two', 'three']#deep_freeze(ary) => Array
Freezes the array and performs a deep freeze on each array item.
ary = ['one', 'two', 'three']
ArrayTools.deep_freeze ary
ary.frozen?
#=> true
ary.first.frozen?
#=> true#fetch(ary, index, default = nil) => Object#fetch(ary, index, &default) => Object#fetch(ary, index, default = nil) => Object
Retrieves the value at the specified index.
If the value does not exist, returns the default value, or raises an IndexError if there is no default value. If the object defines a native #fetch method, delegates to the native implementation.
#fetch(ary, index, &default) => Object
Retrieves the value at the specified index.
If the value does not exist, returns the value of the default block, or raises an IndexError if there is no default block. If the object defines a native #fetch method, delegates to the native implementation.
#humanize_list(ary, **options, &) => String
Generates a human-readable string representation of the list items.
Accepts a list of values and returns a human-readable string of the values, with the format based on the number of items.
With Zero Items
ArrayTools.humanize_list([])
#=> ''With One Item
ArrayTools.humanize_list(['spam'])
#=> 'spam'With Two Items
ArrayTools.humanize_list(['spam', 'eggs'])
#=> 'spam and eggs'With Three Or More Items
ArrayTools.humanize_list(['spam', 'eggs', 'bacon', 'spam'])
#=> 'spam, eggs, bacon, and spam'With Three Or More Items And Options
ArrayTools.humanize_list(
['spam', 'eggs', 'bacon', 'spam'],
:last_separator => ' or '
)
#=> 'spam, eggs, bacon, or spam'#immutable?(ary) => Boolean
Checks if the array and its contents are immutable.
An array is considered immutable if the array itself is frozen and each item in the array is immutable.
ArrayTools.immutable?([1, 2, 3])
#=> false
ArrayTools.immutable?([1, 2, 3].freeze)
#=> true
ArrayTools.immutable?([+'ichi', +'ni', +'san'])
#=> false
ArrayTools.immutable?([+'ichi', +'ni', +'san'].freeze)
#=> false
ArrayTools.immutable?(['ichi', 'ni', 'san'].freeze)
#=> true#mutable?(ary) => Boolean
Checks if the array or any of its contents are mutable.
#splice(ary, start, delete_count, *insert) => Array<Object> (deprecated)
Deprecated. v1.3.0 Use Array#[]= with a range instead.
Replaces a range of items in the array with the given items.
Deleting items from an Array
values = %w(katana wakizashi tachi daito shoto)
ArrayTools.splice values, 1, 2
#=> ['wakizashi', 'tachi']
values
#=> ['katana', 'daito', 'shoto']Inserting items into an Array
values = %w(longsword broadsword claymore)
ArrayTools.splice values, 1, 0, 'zweihander'
#=> []
values
#=> ['longsword', 'zweihander', 'broadsword', 'claymore']Inserting and deleting items
values = %w(shortbow longbow crossbow)
ArrayTools.splice values, 2, 1, 'arbalest', 'chu-ko-nu'
#=> ['crossbow']
values
#=> ['shortbow', 'longbow', 'arbalest', 'chu-ko-nu']#toolbelt => SleepingKingStudios::Tools::Toolbelt
Also known as:
tools
Back to Documentation | Reference | SleepingKingStudios | SleepingKingStudios::Tools