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

···

--
Posted via http://www.ruby-forum.com/.

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

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 Fri, Apr 13, 2012 at 9:31 AM, Soul Surf <lists@ruby-forum.com> 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

···

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

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

···

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

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.

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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

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

···

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

Ralf Mueller 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.

···

--
Posted via http://www.ruby-forum.com/\.

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

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 09:53 AM, Jan E. wrote:
>>

Ralf Mueller 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).

--
Posted via http://www.ruby-forum.com/\.

Ralf Mueller wrote in post #1056271:

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

···

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

--
Posted via http://www.ruby-forum.com/\.