Module: Enumerable
- Defined in:
- lib/darthjee/core_ext/enumerable.rb
Instance Method Summary collapse
- #clean ⇒ Object
-
#clean! ⇒ Object
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 ⇒ Object
4 5 6 |
# File 'lib/darthjee/core_ext/enumerable.rb', line 4 def clean deep_dup.clean! end |
#clean! ⇒ Object
Removes any element that is nil or empty
27 28 29 30 31 32 33 |
# File 'lib/darthjee/core_ext/enumerable.rb', line 27 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
60 61 62 63 64 65 66 |
# File 'lib/darthjee/core_ext/enumerable.rb', line 60 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
87 88 89 90 91 92 |
# File 'lib/darthjee/core_ext/enumerable.rb', line 87 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
103 104 105 106 107 108 109 |
# File 'lib/darthjee/core_ext/enumerable.rb', line 103 def map_to_hash {}.tap do |hash| each do |element| hash[element] = yield(element) end end end |