Method: Enumerable#compact_map
- Defined in:
- lib/core/facets/enumerable/compact_map.rb
#compact_map(&block) ⇒ Object Also known as: compact_collect
A more versitle #compact method. It can be used to collect and filter items out in one single step.
c = [1,2,3].compact_map do |n|
n < 2 ? nil : n
end
c #=> [2,3]
CREDIT: Trans
DEPRECATE: This method should probably be removed b/c #purge does almost the same thing and enum.map{}.compact works too.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/core/facets/enumerable/compact_map.rb', line 17 def compact_map(&block) y = [] if block_given? each do |*a| r = yield(*a) y << r unless r.nil? end else each do |r| y << r unless r.nil? end end y end |