Ruby’s Enumerable module is jam-packed with powerful methods. And knowing how to use them will instantly make you a more efficient Ruby programmer.

Four of the more common Enumerable methods for working with collections are select, reject, any?, and detect.

For example, let’s say we have an array of orders and we want to find all the big orders with totals greater than or equal to $300. We’d use select like so:

orders.select { |o| o.total >= 300 }

Or we could toss out all the small orders with totals less than or equal to $300 using reject:

orders.reject { |o| o.total <= 300 }

If we want to know if some of the orders have a status of pending, we could use any? which returns true or false

orders.any? { |o| o.status == :pending }

Or we could use detect to get a new array containing only the pending orders:

orders.detect { |o| o.status == :pending }

But let’s take it up a notch!

Our Mastering Ruby Blocks & Iterators course includes two videos on tapping into the power of the Enumerable module. Here’s the second video, in whiche we explore three methods that offer handy shortcuts for labor-intensive tasks: partition, map, and reduce:

If you're not using Ruby blocks in your own code, then you're probably doing it wrong.

But don't worrry! Our Mastering Ruby Blocks & Iterators course is your path to mastery! By looking at how Ruby blocks are used in 40+ code examples, you'll level up your Ruby skills instantly. Become a more effective all-around Ruby programmer and really understand both the "how" and the "why" of Ruby blocks and iterators.