如何将此循环转换为for循环?

时间:2015-07-28 20:29:37

标签: ruby

如何将此while循环转换为for循环?

ten_things = "Apples Oranges Crows Telephone Light Sugar"
stuff = ten_things.split(' ')
puts "Wait there are not 10 things in that list. Let's fix that."
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]
while stuff.length != 10
  next_one = more_stuff.pop
  puts "Adding: #{next_one}"
  stuff.push(next_one)
  puts "There are #{stuff.length} items now."
end

我尝试过无限循环

2 个答案:

答案 0 :(得分:2)

我不明白为什么如果TEncoding.GetEncoding(1251)for循环是针对它的话,你会为while循环而烦恼。

然而,这将有效:

until

答案 1 :(得分:2)

您可以使用

array_name.each { |item|
    break if stuff.length > 10
    stuff.push(item)
}