A quick article about how to retrieve an element inside a Ruby array.

· Ruby  · 3 min read

How to find an element inside a Ruby array

A quick article about how to retrieve an element inside a Ruby array.

Use the filter or select method

Like JavaScript, you can choose to .filter an Array

Ruby version

ary = ['abc', 'bzz', 'aaa']
ary.filter{|e| e.include?('a')}
# ['abc', 'aaa']

JS version

let ary = ['abc', 'bzz', 'aaa'];
ary.filter((e) => e.includes('a'));
// ['abc', 'aaa']

Pretty similar right ?

Side note : When manipulating String, I find the .include? (Ruby) and .includes (JS) methods are very confusing. I have a special tip to share here 😬 outside relying on IDE auto-completion. {: .prompt-tip }

Note that .select and .filter are the same, it’s actually an alias - in Ruby! But not in JS.

So both following method are the same :

ary = ['abc', 'bzz', 'aaa']
ary.filter{|e| e.include?('a')}
# ["abc", "aaa"]
ary.select{|e| e.include?('a')}
# ["abc", "aaa"]

Example above emphasises that both are immutable : the ary variable didn’t change between consecutives calls.

Use the include? method

Remember the .include? method about String just above ?

Well, it also works with an Array :

ary = ['abc', 'bzz', 'aaa']
ary.include?('aaa')
# true
ary.include?(42)
# false

Note that the intent is not the same as the paragraph above, so it just depends on what you’re looking for.

How to find the last element inside a Ruby array

“Last” you said ? Well, use the .last method

ary = ['abc', 'bzz', 'aaa']
ary.last
# 'aaa'

You cannot pass a block to the .last method (it would be ignored anyway) {: .prompt-warning }

How to find the first element inside a Ruby array

Well, if you’re just looking for the first element, .first is just here

ary = ['abc', 'bzz', 'aaa']
ary.first
# 'abc'

You cannot pass a block to the .first method (again) {: .prompt-warning }

If you want to find the first element to match a given condition, use .find

Find first matching element

.find allows you to pass a condition, and will return the first element that matches that condition

ary = ['abc', 'bzz', 'aaa']
ary.find{|e| e.include?('z')}
# 'bzz'

How to find the nth matching element

Something I didn’t retrieve trivially is the way to find the nth element to match a given condition.

First and last are easy. First match is easy.

But what about the nth match ?

ary = ['abc', 'bzz', 'aaa']

# Retrive 2nd element with 'a' char
ary.select{|e| e.include?('a')}[1]
# => 'aaa'

# Retrieve 3rd element with 'a' char
ary.select{|e| e.include?('a')}[2]
# => nil

I let you guess how to write a generic function about this - it’s a little bit over-engineering in my humble opinion :

def nth(array, condition, position)
  # ...
end

Retrieve the index of a given element inside a Ruby array

Use the index method like this :

ary = ['abc', 'bzz', 'aaa']
ary.index 'aaa'
# => 2

Note that parenthesis are optional in Ruby when calling a method.

Use of find_index in Ruby arrays

.find_index will retrieve the first element that matches the given condition :

ary = ['abc', 'bzz', 'aaa']
ary.find_index{|e| e.include?('z')}
# => 1

Summary

I wrote this article because I was wondering how to find the nth element that matches a given condition in a Ruby array. It was a good excuse to sum up how to retrieve things inside a Ruby array.

Share:
Back to Blog

Related Posts

View All Posts »
Rails 8 Bootstrap 5 tutorial

Rails 8 Bootstrap 5 tutorial

A very simple tutorial about Rails 8 and Bootstrap 5, since starting with Boostrap is already given by the framework by default with little config.

My honest opinion about Hatchbox

My honest opinion about Hatchbox

Hatchbox.io is a deployment platform for Ruby-on-Rails users. I used it recently, so for people ashamed of trying, here is my review.