---
title: "Rails 6 adds Array#extract!"
description: "Rails 6 adds Array#extract!"
canonical_url: "https://www.bigbinary.com/blog/rails-6-adds-array-extract"
markdown_url: "https://www.bigbinary.com/blog/rails-6-adds-array-extract.md"
---

# Rails 6 adds Array#extract!

Rails 6 adds Array#extract!

- Author: Amit Choudhary
- Published: June 24, 2019
- Categories: Rails 6, Rails

Rails 6 added [extract!](https://github.com/rails/rails/pull/33137) on
[Array](https://api.rubyonrails.org/v5.2/classes/Array.html) class.
[extract!](https://github.com/rails/rails/pull/33137) removes and returns the
elements for which the given block returns true.

[extract!](https://github.com/rails/rails/pull/33137) is different from
[reject!](https://ruby-doc.org/core-2.5.1/Array.html#method-i-reject-21) in the
way that
[reject!](https://ruby-doc.org/core-2.5.1/Array.html#method-i-reject-21) returns
the array after removing the elements whereas
[extract!](https://github.com/rails/rails/pull/33137) returns removed elements
from the array.

Let's checkout how it works.

#### Rails 6.0.0.beta2

Let's pluck all the user emails and then extract emails which include
`gmail.com`.

```ruby

> > emails = User.pluck(:email)
> > SELECT "users"."email" FROM "users"

=> ["amit.choudhary@bigbinary.com", "amit@gmail.com", "mark@gmail.com", "sam@gmail.com"]

> > emails.extract! { |email| email.include?('gmail.com') }

=> ["amit@gmail.com", "mark@gmail.com", "sam@gmail.com"]

> > emails

=> ["amit.choudhary@bigbinary.com"]

> > emails = User.pluck(:email)
> > SELECT "users"."email" FROM "users"

=> ["amit.choudhary@bigbinary.com", "amit@gmail.com", "mark@gmail.com", "sam@gmail.com"]

> > emails.reject! { |email| email.include?('gmail.com') }

=> ["amit.choudhary@bigbinary.com"]

> > emails

=> ["amit.choudhary@bigbinary.com"]
```

Here is the relevant [pull request](https://github.com/rails/rails/pull/33137).

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-adds-array-extract)
