A
A
Anton Misyagin2016-05-31 22:55:00
ruby
Anton Misyagin, 2016-05-31 22:55:00

How to remove array elements nicely in ruby?

[{a: 1, b: 1, c: nil}, {a: 1, b: 2, c: 0}, {a: 2, b: 3, c: 4}]

you need to remove all elements if the element is equal to: {a: 1, b: any, c: any}
Well, something like this from the documentation:
arr = [1, 2, 2, 3]
arr.delete(2) #=> 2
arr #=> [1,3]

those.:
arr = {a: 1, b: 1, c: nil}, {a: 1, b: 2, c: 0}, {a: 2, b: 3, c: 4}
arr.delete {a: 1, b: x, c: x}
arr #=>  [{a: 2, b: 3, c: 4}]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Misyagin, 2016-05-31
@sunnmas

arr.delete_if { |x| x[:a] == 1 }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question