I want to get something like this:
a = [0,1,0,0,1,0,1,0]
a.except(1) # => [0,0,0,1,0,1,0]
a # => [0,1,0,0,1,0,1,0]
a.except(1).except(1) # => [0,0,0,0,1,0]
As you see, the first element of a
that equals the argument of except
is removed from a
.
I can do:
tmp_a = a.dup
tmp_a.delete_at(a.index(1))
tmp_a
but in my opinion, it looks like smell. Is there more elegant one-line solution? (In other words - how to define method "except" for Array?)
UPD
I solved this problem so
class Array
def except(elem)
dup.tap{|a| a.delete_at(a.index(elem))}
end
end
what do you think?