10
Ruby中是否有“ do…while”循环?
我正在使用此代码让用户输入名称,而程序将它们存储在数组中,直到他们输入一个空字符串(他们必须在每个名称后按Enter): 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? 在此代码中,我不必将信息分配给一些随机字符串。 …