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.
Constant Summary collapse
- ARRAY_METHODS =
[:[], :count, :each].freeze
- OTHER_METHODS =
[:each_key, :each_pair].freeze
Instance Method Summary collapse
-
#array?(ary) ⇒ Boolean
Returns true if the object is or appears to be an Array.
-
#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.
-
#deep_freeze(ary) ⇒ Object
Freezes the array and performs a deep freeze on each array item.
-
#humanize_list(ary, options = {}, &block) ⇒ String
Accepts a list of values and returns a human-readable string of the values, with the format based on the number of items.
-
#immutable?(ary) ⇒ Boolean
Returns true if the array is immutable, i.e.
-
#mutable?(ary) ⇒ Boolean
Returns true if the array is mutable.
-
#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
#array?(ary) ⇒ Boolean
Returns true if the object is or appears to be an Array.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 19 def array? ary return true if Array === ary ARRAY_METHODS.each do |method_name| return false unless ary.respond_to?(method_name) end # each OTHER_METHODS.each do |method_name| return false if ary.respond_to?(method_name) end # each true end |
#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.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 55 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}
99 100 101 102 103 104 105 106 107 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 99 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.
115 116 117 118 119 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 115 def deep_dup ary require_array! ary ary.map { |obj| ObjectTools.deep_dup obj } end |
#deep_freeze(ary) ⇒ Object
Freezes the array and performs a deep freeze on each array item.
124 125 126 127 128 129 130 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 124 def deep_freeze ary require_array! ary ary.freeze ary.each { |obj| ObjectTools.deep_freeze obj } end |
#humanize_list(ary, options = {}, &block) ⇒ String
Accepts a list of values and returns a human-readable string of the values, with the format based on the number of items.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 169 def humanize_list ary, = {}, &block require_array! ary ary = ary.map(&block) if block_given? 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 |
#immutable?(ary) ⇒ Boolean
Returns true if the array is immutable, i.e. the array is frozen and each array item is immutable.
203 204 205 206 207 208 209 210 211 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 203 def immutable? ary require_array! ary return false unless ary.frozen? ary.each { |item| return false unless ObjectTools.immutable?(item) } true end |
#mutable?(ary) ⇒ Boolean
Returns true if the array is mutable.
220 221 222 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 220 def mutable? ary !immutable?(ary) 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.
260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 260 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 |