在Ruby中,循环中的返回值是什么?

在Ruby中,循环中的返回值是什么?,第1张

概述使用以下代码: def get_action action = nil until Guide::Config.actions.include?(action) puts "Actions: " + Guide::Config.actions.join(", ") print "> " user_response = gets.chomp 使用以下代码:

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循环返回的值是多少?

解决方法 循环的返回(循环,while,until等)可以是您发送的任何内容

def get_action  loop do    action = gets.chomp    break action if GuIDe::Config.actions.include?(action)  endend

要么

def get_action  while action = gets.chomp    break action if GuIDe::Config.actions.include?(action)  endend

或者你可以使用开始…而

def get_action  begin    action = gets.chomp  end while GuIDe::Config.actions.include?(action)  actionend

甚至更短

def get_action  action = gets.chomp while GuIDe::Config.actions.include?(action)  actionend

PS:循环本身返回nil作为结果(隐式中断,它是break nil),除非你使用显式break“something”.如果你想分配循环的结果你应该使用break:x = loop do break 1;结束

总结

以上是内存溢出为你收集整理的在Ruby中,循环中的返回值是什么?全部内容,希望文章能够帮你解决在Ruby中,循环中的返回值是什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1287667.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存