Behaviour of next in collect

Basically, anyone got a better way to do this? :

irb(main):003:0> [1,2,3,4,5,6].collect { |v| next if v == 3; v * 7 }.compact
=> [7, 14, 28, 35, 42]

Let me know on twitter if you have a better way.

I oversimplified my example, it’s actually more like:

objects.collect do |obj|
  begin
    QueryPredicate.new(obj.parser.parse_internal)
  rescue ParsingException => e
    next
  end
end.compact

Which prevents me using the suggestion of

[1,2,3,4,5,6].reject{|v| v == 3}.map{|v| v * 7}

as I don’t have any way of telling what can be rejected from the set. Still, I learnt the “reject” keyword, which makes me happy.

Suggested improvement from colleague:

objects.collect do |obj|
  begin
    QueryPredicate.new(obj.parser.parse_internal)
  rescue ParsingException => e
    nil
  end
end
objects.compact!

Yeah that’s a bit better