This post originated from an RSS feed registered with Ruby Buzz
by Jamie Hill.
Original Post: A Ruby reject! that returns the rejected items
Feed Title: The Lucid
Feed URL: http://feeds.feedburner.com/thelucid
Feed Description: Lightweight ramblings and tips on Ruby and Rails.
This could be written as a method of the Hash class and an alternative for Array.
For Hash, the code would look something like:
class Hashdef extract!Hash[*self.select{|k,v|yield(k,v)&&self.delete(k)}.flatten]endendoptions={:a=>1,:b=>2,:c=>3}rejects=options.extract!{|k,v|k==:b}assert_equal{:a=>1,:c=>3},optionsassert_equal{:b=>2},rejects
If I am missing something obvious in Ruby that accomplishes the same, please leave a comment.