我有一个获取元素数组的函数,然后迭代数组,当找到期望的元素时它会中断并返回。
功能如下:
def get_expected_element(id:, name:)
# I am sure there are 3 elements got
elem_array = get_all_elements(id)
element = nil
elem_array.each { |elem|
# I see this log
puts "elem = #{elem}"
if elem == name
element = elem
# I see this log too
puts "Found element"
break
end
}
# I see this log too, and program is hanging
puts "=== return ==="
element
end
当我调用该函数时,程序在puts "=== return ==="
:
service = MyService.new
element_got = service.get_expected_element(id:3, name:"apple")
# I don't see the below log
puts "#{element_got}, I don't see this, why?"
登录控制台是这样的:
elem = orange
elem = apple
Found element
=== return ===
<it is hanging>
我无法理解调用函数为什么不返回?
答案 0 :(得分:1)
离开MyService我跑了这个:
elem = elem1
elem = apple
Found element
=== return ===
apple
得到了这个:
ViewPager
你的get_expected_element方法似乎很好。