【发布时间】:2021-09-22 22:43:12
【问题描述】:
我有一系列嵌套的 each 循环遍历卡片列表。这些循环调用其他子函数来测试是否满足某些条件以便继续。
def card_handler
cards.each do |card|
#some non-relevant code is here on my end
already_sent?
end
end
def already_sent?
# allows for checking if different emails have been sent on the same card
if list_action == 147
a_s_helper(p1_label)
elsif list_action == 146
a_s_helper(p2_label)
elsif list_action == 145
a_s_helper(p3_label)
end
end
def a_s_helper(label)
if card::card_labels.include? label
# if the card already has the label, I want to log the error and return all the way to the next card in the iteration
puts '\n Order info: \n id: #{id} \n Email already sent'
next
# doesn't work
else
real_id?
end
end
就像我在 a_s_helper 中的评论中所说,如果卡片已经有标签,我想记录错误并一直返回到迭代中的下一张卡片。我从当前设置中收到“Invalid next”错误。
有没有办法将next 返回到父函数或循环?
【问题讨论】:
标签: ruby-on-rails ruby oop foreach each