vinay
vinay

Reputation: 31

Ruby: if condition inside each condition

I need to compare array element inside "each" condition as shown below:

a = ["config_left","mon_left","acc_left",lg_left..]
a.each { |x| 
  ff.div(:id, x).fireEvent("onmouseup")
  if x == 1   ##<<<<<<<<<<<<is this right? 
    Watir::Waiter::wait_until{ff.button(:id, "add").enabled?}
  else
    sleep 7
end

Is x == 1 right? Tried with x == "mon_left" but even that doesn't work. Please help on it.

Upvotes: 1

Views: 11934

Answers (2)

Frank Schmitt
Frank Schmitt

Reputation: 30765

First of all, you should post a complete example - I had to fix at least 3 syntax errors in your question.

Second, what do you mean by "It doesn't work"? This works for me:

a = ["config_left","mon_left","acc_left","lg_left"]  
a.each_with_index do |x,i|  
  puts i if x == "mon_left"  
end

Third, you might want to use a.detect ... instead of each / if

Upvotes: 6

VMykyt
VMykyt

Reputation: 1629

Looks like you are talking about index:

some_array.each{ | element, index| 
   do_something if index == 1
}

So in your case

a = ["config_left","mon_left","acc_left", "lg_left"] 
a.each { |x, i| 
  ff.div(:id, x).fireEvent("onmouseup")
  if i == 1   
      Watir::Waiter::wait_until{ff.button(:id, "add").enabled?}
  else
      sleep 7
  end
}

Upvotes: 0

Related Questions