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
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
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.
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:
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
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.
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