(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 m
next
I would simply call .compact on the resultant array, which removes any instances of nil in an array. If you'd like it to modify the existing array (no reason not to), use .compact!:
.compact
.compact!
(1..4).collect do |x| next if x == 3 x end.compact!