method
reject
reject()
public
Returns an array for all elements of enum for which block is false (see also Enumerable#find_all).
(1..10).reject {|i| i % 3 == 0 } #=> [1, 2, 4, 5, 7, 8, 10]
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"}