Tom Tom - 3 months ago 8
Ruby Question

Ruby Array Index

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

I have an array called

emails


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

try each_with_index

emails.each_with_index do |email, index|

  if index == 0
    # do something
  end 

end
Comments