(1..4).collect do |x|
next if x == 3
x + 1
end # => [2, 3, nil, 5]
# desired => [2, 3, 5]
If the condition for next
is met, collect
puts nil
in the array, whereas what I'm trying to do is put no element in the returned array if the condition is met. Is this possible without calling delete_if { |x| x == nil }
on the returned array?
My code excerpt is heavily abstracted, so looking for a general solution to the problem.
filter_map
, more info here. – Coble