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

#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