Module: SleepingKingStudios::Tools::ArrayTools
- Extended by:
- ArrayTools
- Included in:
- ArrayTools
- Defined in:
- lib/sleeping_king_studios/tools/array_tools.rb
Overview
Tools for working with array-like enumerable objects.
Instance Method Summary collapse
-
#bisect(ary) {|item| ... } ⇒ Array<Array<Object>>
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.
- #count_values(ary, &block) ⇒ Object
-
#deep_dup(ary) ⇒ Array
Creates a deep copy of the object by returning a new Array with deep copies of each array item.
-
#humanize_list(ary, options = {}) ⇒ String
Accepts a list of values and returns a human-readable string of the values, with the format based on the number of items.
-
#splice(ary, start, delete_count, *insert) ⇒ Array<Object>
Accepts an array, a start value, a number of items to delete, and zero or more items to insert at that index.
Instance Method Details
#bisect(ary) {|item| ... } ⇒ Array<Array<Object>>
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.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 33 def bisect ary, &block require_array! ary raise ArgumentError.new('no block given') unless block_given? selected, rejected = [], [] ary.each do |item| (yield(item) ? selected : rejected) << item end # each [selected, rejected] end |
#count_values(ary) ⇒ Hash{Object, Integer} #count_values(ary) {|item| ... } ⇒ Hash{Object, Integer}
77 78 79 80 81 82 83 84 85 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 77 def count_values ary, &block require_array! ary ary.each.with_object({}) do |item, hsh| value = block_given? ? block.call(item) : item hsh[value] = hsh.fetch(value, 0) + 1 end # each end |
#deep_dup(ary) ⇒ Array
Creates a deep copy of the object by returning a new Array with deep copies of each array item.
93 94 95 96 97 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 93 def deep_dup ary require_array! ary ary.map { |obj| ObjectTools.deep_dup obj } end |
#humanize_list(ary, options = {}) ⇒ String
Accepts a list of values and returns a human-readable string of the values, with the format based on the number of items.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 136 def humanize_list ary, = {} require_array! ary separator = .fetch(:separator, ', ') last_separator = .fetch(:last_separator, ' and ') case ary.count when 0 '' when 1 ary.first.to_s when 2 "#{ary[0]}#{last_separator}#{ary[1]}" else if last_separator =~ /\A,?\s*/ last_separator = last_separator.sub /\A,?\s*/, separator else last_separator = "#{separator}#{last_separator}" end # if-else "#{ary[0...-1].join(separator)}#{last_separator}#{ary.last}" end # case end |
#splice(ary, start, delete_count, *insert) ⇒ Array<Object>
Accepts an array, a start value, a number of items to delete, and zero or more items to insert at that index. Deletes the specified number of items, then inserts the given items at the index and returns the array of deleted items.
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 196 def splice ary, start, delete_count, *insert require_array! ary start = start < 0 ? start + ary.count : start range = start...(start + delete_count) deleted = ary[range] ary[range] = insert deleted end |