Class: Array
- Inherits:
-
Object
- Object
- Array
- Includes:
- Everythingrb::InspectQuotable
- Defined in:
- lib/everythingrb/array.rb
Overview
Extensions to Ruby’s core Array class
Provides:
-
#join_map: Combine filter_map and join operations in one step
-
#key_map, #dig_map: Extract values from arrays of hashes
-
#deep_freeze: Recursively freeze array and contents
-
#compact_prefix, #compact_suffix, #trim_nils: Clean up array boundaries
-
ActiveSupport integrations: #trim_blanks and more when ActiveSupport is loaded
Instance Method Summary collapse
-
#compact_blank_prefix ⇒ Array
Removes blank values from the beginning of an array.
-
#compact_blank_suffix ⇒ Array
Removes blank values from the end of an array.
-
#compact_prefix ⇒ Array
Removes nil values from the beginning of an array.
-
#compact_suffix ⇒ Array
Removes nil values from the end of an array.
-
#deep_freeze ⇒ self
Recursively freezes self and all of its contents.
-
#dig_map(*keys) ⇒ Array
Maps over hash keys to extract nested values using dig.
-
#join_map(join_with = "", with_index: false) {|element, index| ... } ⇒ String
Combines filter_map and join operations.
-
#key_map(key) ⇒ Array
Maps over hash keys to extract values for a specific key.
-
#to_deep_h ⇒ Array
Recursively converts all elements that respond to #to_h.
-
#to_or_sentence(options = {}) ⇒ String
Joins array elements into a sentence with “or” connector.
-
#trim_blanks ⇒ Array
Removes blank values from both the beginning and end of an array.
-
#trim_nils ⇒ Array
Removes nil values from both the beginning and end of an array.
Methods included from Everythingrb::InspectQuotable
Instance Method Details
#compact_blank_prefix ⇒ Array
Removes blank values from the beginning of an array
156 157 158 |
# File 'lib/everythingrb/array.rb', line 156 def compact_blank_prefix drop_while(&:blank?) end |
#compact_blank_suffix ⇒ Array
Removes blank values from the end of an array
169 170 171 |
# File 'lib/everythingrb/array.rb', line 169 def compact_blank_suffix reverse.drop_while(&:blank?).reverse end |
#compact_prefix ⇒ Array
Removes nil values from the beginning of an array
115 116 117 |
# File 'lib/everythingrb/array.rb', line 115 def compact_prefix drop_while(&:nil?) end |
#compact_suffix ⇒ Array
Removes nil values from the end of an array
128 129 130 |
# File 'lib/everythingrb/array.rb', line 128 def compact_suffix reverse.drop_while(&:nil?).reverse end |
#deep_freeze ⇒ self
CAUTION: Be careful when freezing collections that contain class objects or singleton instances - this will freeze those classes/objects globally! Only use deep_freeze on pure data structures you want to make immutable.
Recursively freezes self and all of its contents
101 102 103 104 |
# File 'lib/everythingrb/array.rb', line 101 def deep_freeze each { |v| v.respond_to?(:deep_freeze) ? v.deep_freeze : v.freeze } freeze end |
#dig_map(*keys) ⇒ Array
Maps over hash keys to extract nested values using dig
84 85 86 |
# File 'lib/everythingrb/array.rb', line 84 def dig_map(*keys) map { |v| v.dig(*keys) } end |
#join_map(join_with = "", with_index: false) {|element, index| ... } ⇒ String
Combines filter_map and join operations
45 46 47 48 49 50 51 52 53 |
# File 'lib/everythingrb/array.rb', line 45 def join_map(join_with = "", with_index: false, &block) block = ->(i) { i } if block.nil? if with_index filter_map.with_index(&block).join(join_with) else filter_map(&block).join(join_with) end end |
#key_map(key) ⇒ Array
Maps over hash keys to extract values for a specific key
66 67 68 |
# File 'lib/everythingrb/array.rb', line 66 def key_map(key) map { |v| v[key] } end |
#to_deep_h ⇒ Array
Recursively converts all elements that respond to #to_h
Maps over the array and calls #to_deep_h on any Hash/String elements, #to_h on any objects that respond to it, and handles nested arrays.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/everythingrb/array.rb', line 239 def to_deep_h map do |value| case value when Hash value.to_deep_h when Array value.to_deep_h when String # If the string is not valid JSON, #to_deep_h will return `nil` value.to_deep_h || value else value.respond_to?(:to_h) ? value.to_h : value end end end |
#to_or_sentence(options = {}) ⇒ String
Joins array elements into a sentence with “or” connector
Similar to ActiveSupport’s #to_sentence but uses “or” instead of “and” as the joining word between elements.
204 205 206 207 |
# File 'lib/everythingrb/array.rb', line 204 def to_or_sentence( = {}) = .reverse_merge(last_word_connector: ", or ", two_words_connector: " or ") to_sentence() end |
#trim_blanks ⇒ Array
Removes blank values from both the beginning and end of an array
182 183 184 |
# File 'lib/everythingrb/array.rb', line 182 def trim_blanks compact_blank_prefix.compact_blank_suffix end |
#trim_nils ⇒ Array
Removes nil values from both the beginning and end of an array
141 142 143 |
# File 'lib/everythingrb/array.rb', line 141 def trim_nils compact_prefix.compact_suffix end |