From: Ralf Mueller Date: 2012-04-13T16:38:24+09:00 Subject: Re: How to test if array element exists? This is a multi-part message in MIME format. --------------080605070806000607010508 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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 > Forgotten: For such things, the online doc is very helpful: http://www.ruby-doc.org/core-1.9.3/Array.html Many of the things you might need, may be already there ralf --------------080605070806000607010508 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit 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

Forgotten: For such things, the online doc is very helpful: http://www.ruby-doc.org/core-1.9.3/Array.html
Many of the things you might need, may be already there

ralf
--------------080605070806000607010508--