DEV Community

Josh Phelps
Josh Phelps

Posted on

1

Ruby Methods: Each, Map, Select, and Reject Explained

Here are four Ruby methods you should know when working with arrays:

Each vs. Map

The main difference between map and each is in the return value. Using each will return the original array while map will return a new array.

Example:

Alt Text

Select

The select method will return a new array that contains the elements of the original array for which the given block returns a true value.

Example:

Alt Text

In this example, we selected all the elements in the original array unless the element equaled "a".

Here is another example:

Alt Text

Reject

Reject is the opposite of select. It will return a new array that contains the elements of the original array for which the given block returns a value that is not true.

Example:

Alt Text

In this example, we are returning all the odd numbers in the original array.

There are many more methods that can be called on an array. For more array methods visit https://ruby-doc.org/core-2.4.1/Array.html

Thanks!

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

AWS Industries LIVE! Stream

Watch AWS Industries LIVE!

Watch Industries LIVE! to find out how the AWS cloud helps solve real-world business challenges.

Learn More

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay