Class: Array

Inherits:
Object
  • Object
show all
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

  • #compact_prefix, #compact_suffix, #trim_nils: Clean up array boundaries

  • ActiveSupport integrations: #trim_blanks and more when ActiveSupport is loaded

Examples:

require "everythingrb/array"
["foo", nil, "bar"].join_map(", ") { |s| s&.upcase }  # => "FOO, BAR"
[{name: "Alice"}, {name: "Bob"}].key_map(:name)  # => ["Alice", "Bob"]

Instance Method Summary collapse

Methods included from Everythingrb::InspectQuotable

#in_quotes

Instance Method Details

#compact_blank_prefixArray

Removes blank values from the beginning of an array

Examples:

With ActiveSupport loaded

[nil, "", 1, 2, "", 3].compact_blank_prefix
# => [1, 2, "", 3]


138
139
140
# File 'lib/everythingrb/array.rb', line 138

def compact_blank_prefix
  drop_while(&:blank?)
end

#compact_blank_suffixArray

Removes blank values from the end of an array

Examples:

With ActiveSupport loaded

[1, 2, "", 3, nil, ""].compact_blank_suffix
# => [1, 2, "", 3]


151
152
153
# File 'lib/everythingrb/array.rb', line 151

def compact_blank_suffix
  reverse.drop_while(&:blank?).reverse
end

#compact_prefixArray

Removes nil values from the beginning of an array

Examples:

[nil, nil, 1, 2, nil, 3].compact_prefix
# => [1, 2, nil, 3]


97
98
99
# File 'lib/everythingrb/array.rb', line 97

def compact_prefix
  drop_while(&:nil?)
end

#compact_suffixArray

Removes nil values from the end of an array

Examples:

[1, 2, nil, 3, nil, nil].compact_suffix
# => [1, 2, nil, 3]


110
111
112
# File 'lib/everythingrb/array.rb', line 110

def compact_suffix
  reverse.drop_while(&:nil?).reverse
end

#dig_map(*keys) ⇒ Array

Maps over hash keys to extract nested values using dig

Examples:

users = [
  {user: {profile: {name: "Alice"}}},
  {user: {profile: {name: "Bob"}}}
]
users.dig_map(:user, :profile, :name)
# => ["Alice", "Bob"]


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

Examples:

Without index

[1, 2, nil, 3].join_map(" ") { |n| n&.to_s if n&.odd? }
# => "1 3"

With index

["a", "b", "c"].join_map(", ", with_index: true) { |char, i| "#{i}:#{char}" }
# => "0:a, 1:b, 2:c"

Default behavior without block

[1, 2, nil, 3].join_map(", ")
# => "1, 2, 3"

Yields:

  • (element, index)

    Block that filters and transforms array elements

Yield Parameters:

  • element (Object)

    The current element

  • index (Integer)

    The index of the current element (only if with_index: true)



44
45
46
47
48
49
50
51
52
# File 'lib/everythingrb/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

Examples:

[{name: "Alice", age: 30}, {name: "Bob", age: 25}].key_map(:name)
# => ["Alice", "Bob"]


65
66
67
# File 'lib/everythingrb/array.rb', line 65

def key_map(key)
  map { |v| v[key] }
end

#to_deep_hArray

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.

Examples:

Converting arrays with mixed object types

users = [
  {name: "Alice", roles: ["admin"]},
  OpenStruct.new(name: "Bob", active: true),
  Data.define(:name).new(name: "Carol")
]
users.to_deep_h
# => [
#      {name: "Alice", roles: ["admin"]},
#      {name: "Bob", active: true},
#      {name: "Carol"}
#    ]

With nested arrays and JSON strings

data = [
  {profile: '{"level":"expert"}'},
  [OpenStruct.new(id: 1), OpenStruct.new(id: 2)]
]
data.to_deep_h
# => [{profile: {level: "expert"}}, [{id: 1}, {id: 2}]]


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/everythingrb/array.rb', line 221

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.

Examples:

Basic usage

["red", "blue", "green"].to_or_sentence
# => "red, blue, or green"

With only two elements

["yes", "no"].to_or_sentence
# => "yes or no"


186
187
188
189
# File 'lib/everythingrb/array.rb', line 186

def to_or_sentence(options = {})
  options = options.reverse_merge(last_word_connector: ", or ", two_words_connector: " or ")
  to_sentence(options)
end

#trim_blanksArray

Removes blank values from both the beginning and end of an array

Examples:

With ActiveSupport loaded

[nil, "", 1, 2, "", 3, nil, ""].trim_blanks
# => [1, 2, "", 3]


164
165
166
# File 'lib/everythingrb/array.rb', line 164

def trim_blanks
  compact_blank_prefix.compact_blank_suffix
end

#trim_nilsArray

Removes nil values from both the beginning and end of an array

Examples:

[nil, nil, 1, 2, nil, 3, nil, nil].trim_nils
# => [1, 2, nil, 3]


123
124
125
# File 'lib/everythingrb/array.rb', line 123

def trim_nils
  compact_prefix.compact_suffix
end