How to test if array element exists?

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

On Fri, Apr 13, 2012 at 9:31 AM, Soul S. [email protected] wrote:

end
if array[4].nil?

What you are doing above is creating an array with one element, which
is never nil:

[nil].nil?

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

Jesus.

On 04/13/2012 09:31 AM, Soul S. wrote:

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

On 04/13/2012 09:31 AM, Soul S. wrote:

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

Forgotten: For such things, the online doc is very helpful:

Many of the things you might need, may be already there

ralf

thanks. let me change the array to make it more clear what i’m trying to
test …

M=‘M’
array = [M,M,M,M]

if [array[4]].empty?
puts ‘empty’
end
if [array[4]].nil?
puts ‘nil’
end

my array has 4 elements, all of them are ‘M’. array[0] is M … to
array[3] is M … but array[4] doesn’t exist. what if statement can I
write that will tell me that array[4] doesn’t exist? the two if
statements above return false.

Ralf M. wrote in post #1056263:

Forgotten: For such things, the online doc is very helpful:
Class: Array (Ruby 1.9.3)
Many of the things you might need, may be already there

ralf

thanks. that’s where i got the .empty? … but like i said that is
returning false. i just want to know a way to determine that the element
does not exist in the array, not the actual value.

Hi,

If you access a non-existing index, you get nil. That is, your
expression [array[4]] evaluates to [nil], which is not empty (but has
exactly one element).

The only way to test for existence is to check the length of the array:

if 4 >= array.length
puts ‘no index 4’
end

However, the length doesn’t mean that the array has actually been
populated up to length - 1. It only means that length - 1 is the biggest
index that has been set:

array = []
array[4] = ‘x’
puts array.length # this is 5

Ralf M. wrote in post #1056271:

On 04/13/2012 09:53 AM, Jan E. wrote:

end

what about this
?> array = [0,1,2,3]
=> [0, 1, 2, 3]

array[4]
=> nil

array[4].nil?
=> true

array[444].nil?
=> true

array[2].nil?
=> false

This doesn’t work, because nil may also be an actual element:

array = [nil]
puts array[0].nil? # true
puts array[1].nil? # true

So you cannot reliably tell if a certain element has been set (apart
from the length solution mentioned above).

On 04/13/2012 09:53 AM, Jan E. wrote:

end

what about this
?> array = [0,1,2,3]
=> [0, 1, 2, 3]

array[4]
=> nil

array[4].nil?
=> true

array[444].nil?
=> true

array[2].nil?
=> false

On 04/13/2012 10:31 AM, Soul S. wrote:

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

Answer depends on what you want from check
here is array arr = [1,2,nil,nil]
puts arr[2]

nil
puts arr[3]

nil

You can have nil values as element, so maybe better check arr.length <
your_element_number

Ralf M. wrote in post #1056271:

On 04/13/2012 09:53 AM, Jan E. wrote:

end

what about this
?> array = [0,1,2,3]
=> [0, 1, 2, 3]

array[4]
=> nil

array[4].nil?
=> true

array[444].nil?
=> true

array[2].nil?
=> false

thanks. i found part of my problem was

[array[4]].nil? => false

whereas

array[4].nil? => true

it was a syntax error.

the .length is good also

thanks