Class: Array

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

Examples:

Using the extensions

numbers = [1, 2, nil, 3]

# Filter out nils and format odd numbers
numbers.join_map(", ") { |n| "odd: #{n}" if n&.odd? }
# => "odd: 1, odd: 3"

users = [{name: "Alice", role: "admin"}, {name: "Bob", role: "user"}]
users.key_map(:name)    # => ["Alice", "Bob"]

Instance Method Summary collapse

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]

Returns:

  • (Array)

    Array with leading blank values removed



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

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]

Returns:

  • (Array)

    Array with trailing blank values removed



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

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]

Returns:

  • (Array)

    Array with leading nil values removed



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

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]

Returns:

  • (Array)

    Array with trailing nil values removed



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

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

#deep_freezeself

Recursively freezes self and all of its contents

Examples:

Freeze an array with nested structures

["hello", { name: "Alice" }, [1, 2, 3]].deep_freeze
# => All elements and nested structures are now frozen

Returns:

  • (self)

    Returns the frozen array



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

Examples:

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

Parameters:

Returns:

  • (Array)

    Array of nested values



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

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"

Parameters:

  • join_with (String) (defaults to: "")

    The delimiter to join elements with (defaults to empty string)

  • with_index (Boolean) (defaults to: false)

    Whether to include the index in the block (defaults to false)

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)

Returns:

  • (String)

    Joined string of filtered and transformed elements



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

Examples:

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

Parameters:

Returns:

  • (Array)

    Array of values extracted from each hash



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

def key_map(key)
  map { |v| v[key] }
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]

Returns:

  • (Array)

    Array with leading and trailing blank values removed



177
178
179
# File 'lib/everythingrb/core/array.rb', line 177

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]

Returns:

  • (Array)

    Array with leading and trailing nil values removed



136
137
138
# File 'lib/everythingrb/core/array.rb', line 136

def trim_nils
  compact_prefix.compact_suffix
end