1
我有一個函數,它獲取一個元素的數組,然後它遍歷數組,當預期的元素被發現它打破並返回。函數不返回,很奇怪
的功能是這樣的:
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>
我不明白爲什麼被調用的函數不會返回?
你試過了顯式的return語句嗎? (即返回元素)。我不明白爲什麼這可能會有所作爲,但它首先很容易嘗試。 – dean
是的,嘗試過,同樣的問題 –