首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Ruby中有“do ... while”循环吗?

Ruby中有“do ... while”循环吗?

提问于 2017-12-20 15:23:05
回答 2关注 0查看 333

我使用这个代码让用户输入名字,而程序将它们存储在一个数组中,直到他们输入一个空字符串(他们必须在每个名字之后按回车键):

people = []
info = 'a' # must fill variable with something, otherwise loop won't execute
while not info.empty?
    info = gets.chomp
    people += [Person.new(info)] if not info.empty?
end

这段代码在do ... while循环中看起来好多了:

people = []
do
    info = gets.chomp
    people += [Person.new(info)] if not info.empty?
while not info.empty?

在这个代码中,我不必将信息分配给一些随机字符串。

不幸的是,这种类型的循环似乎并不存在于Ruby中。任何人都可以提出一个更好的方法来做到这一点?

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档