I have an array in which I want to filter its elements, and mutate it to remove that elements, not only that I need that filtering to return a new array with the filtered elements, something like this:
array = [1, 2]
rejected_elements = array.rejection_method! {|a| a == 1} => [1]
rejected_elements => [1]
array => [2]
Is there any built in method in Ruby to do so?
Here is what I have tried:
dupped_array = array.dup
rejected_elements = array.reject! {|a| a == 1}
array = dupped_array - rejected_elements
But I have an array that contains nested hashes, and duplicating it won't be a good idea, and will cause me hell. So I asked if there is any built in method or good way to do this straight forward.