Module: Enumerable
- Defined in:
- lib/darthjee/core_ext/enumerable.rb
Instance Method Summary collapse
-
#clean ⇒ ::Enumerable
Removes any element that is nil or empty.
-
#clean! ⇒ ::Enumerable
Removes any element that is nil or empty.
-
#map_and_find {|*args| ... } ⇒ Object
Maps the elements into a new value, returning the first element that is evaluated to true.
-
#map_and_select {|*args| ... } ⇒ Object
Maps the elements into a new value returning an array of the values mapped to non false values.
-
#map_to_hash {|*args| ... } ⇒ Object
Maps values and creates a hash whose values are the result of the #map and the keys are the original values.
Instance Method Details
#clean ⇒ ::Enumerable
Removes any element that is nil or empty
This method does not change the original enumerable
8 9 10 |
# File 'lib/darthjee/core_ext/enumerable.rb', line 8 def clean deep_dup.clean! end |
#clean! ⇒ ::Enumerable
Removes any element that is nil or empty
31 32 33 34 35 36 37 |
# File 'lib/darthjee/core_ext/enumerable.rb', line 31 def clean! if is_a?(Hash) delete_if { |_k, v| empty_value?(v) } else delete_if { |v| empty_value?(v) } end end |
#map_and_find {|*args| ... } ⇒ Object
Maps the elements into a new value, returning the first element 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
64 65 66 67 68 69 70 |
# File 'lib/darthjee/core_ext/enumerable.rb', line 64 def map_and_find mapped = nil find do |*args| mapped = yield(*args) end mapped || nil end |
#map_and_select {|*args| ... } ⇒ Object
Maps the elements into a new value returning an array of the values mapped to non false values
This method is equivalent to call #map#select
91 92 93 94 95 96 |
# File 'lib/darthjee/core_ext/enumerable.rb', line 91 def map_and_select mapped = map do |*args| yield(*args) end mapped.select { |e| e } end |
#map_to_hash {|*args| ... } ⇒ Object
Maps values and creates a hash whose values are the result of the #map and the keys are the original values
107 108 109 110 111 112 113 |
# File 'lib/darthjee/core_ext/enumerable.rb', line 107 def map_to_hash {}.tap do |hash| each do |element| hash[element] = yield(element) end end end |