Kang-Kyu Lee

February 6, 2025

Ruby detect

detect can find one fits some condition, in a list

find can detect one element comes first for that

"find" is "detect" in Ruby language

For example:

numbers = [0, 4, 2, 3, 9, 8]

numbers.find {|n| n.odd? } # first odd number in the list is 3
#=> 3
numbers.find {|n| n > 10 } # there is no number greater than 10 in the list
#=> nil

See you next time!