Filter to exclude elements from array

Array#excluding (Rails 6+)

If you are using Rails (or a standalone ActiveSupport), starting from version 6, there is a Array#excluding method which returns a copy of the array excluding the specified elements.

["David", "Rafael", "Aaron", "Todd"].excluding("Aaron", "Todd") # => ["David", "Rafael"]
[ [ 0, 1 ], [ 1, 0 ] ].excluding([ [ 1, 0 ] ]) # => [ [ 0, 1 ] ]

So, your example can be rewritten as follows:

total = ['alpha', 'bravo', 'charlie', 'delta', 'echo']
hide = ['charlie', 'echo']

pick = total.excluding(hide)
# => ['alpha', 'bravo', 'delta']

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)