This happens with v1.9.2:
my_array.map.with_index{ |e,i| (e.nil?) ? i : nil }.compact
=> [3, 4, 6, 8]
The question changed when I answered, so this corresponds to the question that is now:
my_array.map.with_index{ |e,i| (e.nil?) ? nil : i }.compact
=> [0, 1, 2, 5, 7]
This is just a case of switching the values ββof the ternary operator around.
And again the question has changed. With 1.8.7 and 1.9.2:
ruby-1.8.7-p330 :004 > my_array.each_with_index.map{|e,i| (e.nil?) ? nil : i }.compact
=> [0, 1, 2, 5, 7]
ruby-1.9.2-p136 :002 > my_array.each_with_index.map{|e,i| (e.nil?) ? nil : i }.compact
=> [0, 1, 2, 5, 7]
source
share