Class: SleepingKingStudios::Tools::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 =
Expected methods that an Array-like object should implement.
i[[] count each].freeze
- OTHER_METHODS =
Methods that an Array-like object should not implement.
i[each_key each_pair].freeze
Instance Method Summary collapse
-
#array?(obj) ⇒ Boolean
Returns true if the object is or appears to be an Array.
-
#bisect(ary) {|item| ... } ⇒ Array<Array<Object>>
Partitions the array into matching and non-matching items.
-
#count_values(ary, &block) ⇒ Object
(also: #tally)
Counts the number of times each item or result appears in the object.
-
#deep_dup(ary) ⇒ Array
Creates a deep copy of the object.
-
#deep_freeze(ary) ⇒ Array
Freezes the array and performs a deep freeze on each array item.
-
#humanize_list(ary, **options) ⇒ String
Generates a human-readable string representation of the list items.
-
#immutable?(ary) ⇒ Boolean
Checks if the array and its contents are immutable.
-
#mutable?(ary) ⇒ Boolean
Checks if the array or any of its contents are mutable.
-
#splice(ary, start, delete_count, *insert) ⇒ Array<Object>
Replaces a range of items in the array with the given items.
Methods inherited from Base
Instance Method Details
#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.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 47 def array?(obj) return true if obj.is_a?(Array) ARRAY_METHODS.each do |method_name| return false unless obj.respond_to?(method_name) end OTHER_METHODS.each do |method_name| return false if obj.respond_to?(method_name) end true end |
#bisect(ary) {|item| ... } ⇒ Array<Array<Object>>
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.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 85 def bisect(ary) require_array!(ary) raise ArgumentError, 'no block given' unless block_given? selected = [] rejected = [] ary.each do |item| (yield(item) ? selected : rejected) << item end [selected, rejected] end |
#count_values(ary) ⇒ Hash{Object=>Integer} #count_values(ary) {|item| ... } ⇒ Hash{Object=>Integer} Also known as: tally
Counts the number of times each item or result appears in the object.
133 134 135 136 137 138 139 140 141 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 133 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 end |
#deep_dup(ary) ⇒ Array
Creates a deep copy of the object.
Iterates over the array and returns a new Array with deep copies of each array item.
171 172 173 174 175 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 171 def deep_dup(ary) require_array!(ary) ary.map { |obj| ObjectTools.deep_dup obj } end |
#deep_freeze(ary) ⇒ Array
Freezes the array and performs a deep freeze on each array item.
195 196 197 198 199 200 201 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 195 def deep_freeze(ary) require_array!(ary) ary.freeze ary.each { |obj| ObjectTools.deep_freeze obj } end |
#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.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 245 def humanize_list(ary, **, &) require_array!(ary) return '' if ary.empty? size = ary.size ary = ary.map(&) if block_given? return ary[0].to_s if size == 1 separator, last_separator = (size:, **) return "#{ary[0]}#{last_separator}#{ary[1]}" if size == 2 "#{ary[0...-1].join(separator)}#{last_separator}#{ary.last}" end |
#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.
293 294 295 296 297 298 299 300 301 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 293 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
Checks if the array or any of its contents are mutable.
313 314 315 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 313 def mutable?(ary) !immutable?(ary) end |
#splice(ary, start, delete_count, *insert) ⇒ Array<Object>
Replaces a range of items in the array with the given items.
350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/sleeping_king_studios/tools/array_tools.rb', line 350 def splice(ary, start, delete_count, *insert) require_array!(ary) start += ary.count if start.negative? range = start...(start + delete_count) deleted = ary[range] ary[range] = insert deleted end |