使用以下代碼:在Ruby中,循環中的返回值是什麼?
def get_action
action = nil
until Guide::Config.actions.include?(action)
puts "Actions: " + Guide::Config.actions.join(", ")
print "> "
user_response = gets.chomp
action = user_response.downcase.strip
end
return action
end
下面的代碼把用戶的響應,並最終返回其行動的另一種方法。我知道一個循環會重複直到它最終破裂,但對返回值很好奇,所以我可以更好地構造下一次循環。在until
循環中,我很想知道until
循環返回的值是什麼,如果有返回值呢?
你是問'x'會是什麼,如果你'x = until .... end'? –
詢問函數返回值是什麼,直到某個條件.... end(甚至不一定是這個例子,但一般情況下),類似於方法的返回值如何作爲最後一行或何時使用'return'關鍵字 – developer098
'return'打破循環。按照mits的說法賦值'x = ... etc',將'nil'賦給'x'。 –