From: Ralf Mueller Date: 2012-04-13T16:36:13+09:00 Subject: Re: How to test if array element exists? On 04/13/2012 09:31 AM, Soul Surf wrote: > I'm new to Ruby and just want to test if an array element exists. Here's > a basic code snippet: > > array = [0,1,2,3] > > if [array[4]].empty? > puts 'empty' > end > if [array[4]].nil? > puts 'nil' > end > > element 4 does not exist in the array, but neither of the if statements > result in true? what would be an if statement to check that array[4] > does not exist? Thanks > array.include? is what you need: >> array = [0,1,2,3] => [0, 1, 2, 3] >> array.include?(4) => false >> array.include?(3) => true >> used ruby-1.9.3