Tom Tom - 1 year ago 119
Ruby Question

Ruby Array Index

I have what will probably become a simple question to answer.

I have an array called


I'm iterating over them using
emails.each do |email|

what I want to say is:

if array index is 0, do this.

I've tried
if email.index == 0

I've tried to find a solution, but can't for the life of me find it. As it's only one argument, I'd like to avoid a case statement.

Answer Source

try each_with_index

emails.each_with_index do |email, index|

  if index == 0
    # do something

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download