method

reject

v1_9_2_180 - Show latest stable - Class: Enumerable
reject()
public

Returns an array for all elements of enum for which block is false (see also Enumerable#find_all).

If no block is given, an enumerator is returned instead.

(1..10).reject {|i|  i % 3 == 0 }   #=> [1, 2, 4, 5, 7, 8, 10]

1Note

Using reject to remove key/value pairs from a hash

bobfirestone ยท Sep 9, 20131 thank

==== Code example # Remove empty strings { a: 'first', b: '', c: 'third' }.reject { |k,v| v.empty? } #=> {:a=>"first", :c=>"third"}

# Remove nil
{a: 'first', b: nil, c: 'third'}.reject { |k,v| v.nil? } # => {:a=>"first", :c=>"third"}

# Remove nil & empty strings
{a: '', b: nil, c: 'third'}.reject { |k,v| v.nil? || v.empty? } # => {:c=>"third"}