Ruby: if condition inside each condition
I need to compare array element inside "开发者_C百科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.
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
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
}
精彩评论