method
reject
reject()
public
Returns an array for all elements of enum for which the given block returns false.
If no block is given, an Enumerator is returned instead.
(1..10).reject { |i| i % 3 == 0 } #=> [1, 2, 4, 5, 7, 8, 10] [1, 2, 3, 4, 5].reject { |num| num.even? } #=> [1, 3, 5]
See also Enumerable#find_all.
Register or
log in
to add new notes.
bobfirestone -
September 9, 2013
1 thank
Using reject to remove key/value pairs from a hash
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"}