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 only one.
-
#map_and_select {|*args| ... } ⇒ ::Array<::Object>
Maps the elements into a new value returning a subset.
-
#map_to_hash {|*args| ... } ⇒ ::Hash
Maps values and creates a hash.
Instance Method Details
#clean ⇒ ::Enumerable
Removes any element that is nil or empty
This method does not change the original enumerable
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
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
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
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
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 |