This blog is part of our Rails 6 series.
Rails 6 added extract! on Array class. extract! removes and returns the elements for which the given block returns true.
extract! is different from reject! in the way that reject! returns the array after removing the elements whereas extract! returns removed elements from the array.
Let's checkout how it works.
Let's pluck all the user emails and then extract emails which include
gmail.com.
> > emails = User.pluck(:email)
> > SELECT "users"."email" FROM "users"
=> ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
> > emails.extract! { |email| email.include?('gmail.com') }
=> ["[email protected]", "[email protected]", "[email protected]"]
> > emails
=> ["[email protected]"]
> > emails = User.pluck(:email)
> > SELECT "users"."email" FROM "users"
=> ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
> > emails.reject! { |email| email.include?('gmail.com') }
=> ["[email protected]"]
> > emails
=> ["[email protected]"]
Here is the relevant pull request.

Written byAmit Choudhary
Related blogs
- 01Rails 6 adds support to persist timezones of Active JobSeptember 1, 2020 · Chetan Gawai
- 02Rails 6 adds including & excluding method on EnumerablesAugust 18, 2020 · Akhil Gautam
- 03Rails 6 fixes after_commit callback invocation bugFebruary 25, 2020 · Amit Choudhary
- 04Rails 6 adds rails db:prepare to migrate or setup a databaseDecember 10, 2019 · Akhil Gautam