Module: Enumerable

Defined in:
lib/darthjee/core_ext/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#clean::Enumerable

Removes any element that is nil or empty

This method does not change the original enumerable

Examples:

cleaning a Hash

hash = {
  keep: 'value',
  nil_value: nil,
  empty_array: [],
  empty_string: '',
  empty_hash: {}
}

hash.clean  # returns { keep: 'value' } without changing the hash

cleaning an Array

array = ['value', nil, [], '', {}]

array.clean  # returns ['value'] without changing the array

Returns:

See Also:



28
29
30
# File 'lib/darthjee/core_ext/enumerable.rb', line 28

def clean
  deep_dup.clean!
end

#clean!::Enumerable

Removes any element that is nil or empty

Examples:

cleaning a Hash

hash = {
  keep: 'value',
  nil_value: nil,
  empty_array: [],
  empty_string: '',
  empty_hash: {}
}

hash.clean! # changes the hash to { keep: 'value' }

cleaning an Array

array = ['value', nil, [], '', {}]

array.clean! # changes the array to be  ['value']

Returns:



53
54
55
# File 'lib/darthjee/core_ext/enumerable.rb', line 53

def clean!
  delete_if { |*args| empty_value?(args.last) }
end

#map_and_find {|*args| ... } ⇒ ::Object

Maps the elements into a new value, returning only one

The result to be returned is the first mapping that is evaluated to true

This method is equivalent to #map#find but only calling the map block up to when a value is found

Examples:

Using an array of keys to remove remove elements of a hash


service_map = {
  a: nil,
  b: false,
  c: 'found me',
  d: nil,
  e: 'didnt find me'
}

keys = %i[a b c d e]

keys.map_and_find do |key|   #
  service_values.delete(key) #
end                          # returns 'found me'

service_map # has lost only 3 keys returning
            # { d: nil, e: 'didnt find me' }

Yields:

  • (*args)

    mappig block

Returns:



88
89
90
91
92
93
94
# File 'lib/darthjee/core_ext/enumerable.rb', line 88

def map_and_find
  mapped = nil
  find do |*args|
    mapped = yield(*args)
  end
  mapped || nil
end

#map_and_select {|*args| ... } ⇒ ::Array<::Object>

Maps the elements into a new value returning a subset

The subset returned has the values mapped to non false values

This method is equivalent to call #map#select

Examples:

Mapping the values of hash to their size

hash = {
  a: nil,
  b: 'aka',
  c: 'a'
}

values = hash.map_and_select do |key, value|
  value && value.to_s
end

values # returns [3, 1]

Yields:

  • (*args)

    mapping block

Returns:



119
120
121
# File 'lib/darthjee/core_ext/enumerable.rb', line 119

def map_and_select(&block)
  map(&block).select(&:trueful?)
end

#map_to_hash {|*args| ... } ⇒ ::Hash

Maps values and creates a hash

The keys will be the original values used in the mapping and the values the result of the #map

Examples:

Mapping strings to their sizes

strings =  %w[word big_word]

strings.map_to_hash(&:size) # returns { 'word' => 4, 'big_word' => 8 }

Yields:

  • (*args)

    the mapping block

Returns:



136
137
138
139
140
# File 'lib/darthjee/core_ext/enumerable.rb', line 136

def map_to_hash
  map do |value|
    [value, yield(value)]
  end.to_h
end