• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Find elements in a ruby array

Posted Feb 2, 2023 โ€” Updated Jan 10, 2024

There are several ways to do this, and their outputs differ based on your needs

  • .include? – returns true if the element is found, false if not
  • .index – returns the index of the first occurrence of the element in the array, or nil if it isn’t found
  • .find, .detect– Interchangeable. Return the first element which matches the criteria in the passed block, or nil if nothing matches
  • .select, .filter, .find_all – Interchangeable. All return an array containing all elements which match the criteria in the passed block, returns [] if no matches are found.

Let’s do a couple of examples.

Array.include?

Simple:

numbers = [1, 2, 3, 4]
numbers.include?(2) # true
numbers.include?("hi") # false

Array.index

This method is a bit confusingly named. It gives you the first index at which the given element appears, not the element at the index given.

numbers = [1, 2, 3, 2, 1]
numbers.index(2) # 1
numbers.index(4) # nil

Array.find and Array.detect

Two names for the same method, both return the first matching element:

numbers = [1, 2, 3, 2]

numbers.find { |x| x.even? } # 2 (yes, ruby has built-in .even? and .odd?)
numbers.find { |x| x == "hi" } # nil

numbers.detect { |x| x.even? } # 2
numbers.detect { |x| x == "hi" } # nil

Array.select, Array.filter, Array. find_all

These three all behave identically when operating on arrays11.

numbers = [1, 2, 3, 4, 5]

numbers.select { |x| x.even? } # [2, 4]
numbers.select { |x| x > 10 } # []

numbers.filter { |x| x.even? } # [2, 4]
numbers.filter { |x| x > 10 } # []

numbers.find_all { |x| x.even? } # [2, 4]
numbers.find_all { |x| x > 10 } # []

In most languages, this method is called filter. Filter was not originally a method in ruby, but it was added in version 2.6.3 in April 2019.


Notes

  1. filter does not exist on ruby hashes, and find_all and select have different return types when working with hashes. For more detail, go here. โ†ฉ๏ธŽ

Helpful Links

  • Differences between find_all and select

Filed Under: Arrays, Ruby

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 ยท Contact me at justin [at] {this domain}

  • Privacy Policy