Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/everythingrb/core/array.rb
Overview
Extensions to Ruby’s core Array class
This module adds convenient mapping, joining, and freezing functionality to all Arrays in your application.
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.
-
#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.
Instance Method Details
#compact_blank_prefix ⇒ Array
Removes blank values from the beginning of an array
151 152 153 |
# File 'lib/everythingrb/core/array.rb', line 151 def compact_blank_prefix drop_while(&:blank?) end |
#compact_blank_suffix ⇒ Array
Removes blank values from the end of an array
164 165 166 |
# File 'lib/everythingrb/core/array.rb', line 164 def compact_blank_suffix reverse.drop_while(&:blank?).reverse end |
#compact_prefix ⇒ Array
Removes nil values from the beginning of an array
110 111 112 |
# File 'lib/everythingrb/core/array.rb', line 110 def compact_prefix drop_while(&:nil?) end |
#compact_suffix ⇒ Array
Removes nil values from the end of an array
123 124 125 |
# File 'lib/everythingrb/core/array.rb', line 123 def compact_suffix reverse.drop_while(&:nil?).reverse end |
#deep_freeze ⇒ self
Recursively freezes self and all of its contents
96 97 98 99 |
# File 'lib/everythingrb/core/array.rb', line 96 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
83 84 85 |
# File 'lib/everythingrb/core/array.rb', line 83 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
44 45 46 47 48 49 50 51 52 |
# File 'lib/everythingrb/core/array.rb', line 44 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
65 66 67 |
# File 'lib/everythingrb/core/array.rb', line 65 def key_map(key) map { |v| v[key] } end |
#trim_blanks ⇒ Array
Removes blank values from both the beginning and end of an array
177 178 179 |
# File 'lib/everythingrb/core/array.rb', line 177 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
136 137 138 |
# File 'lib/everythingrb/core/array.rb', line 136 def trim_nils compact_prefix.compact_suffix end |