Ruby 使用'each'而不是'for'-'each'循环
以重写脚本的艰难方式学习Ruby:Ruby 使用'each'而不是'for'-'each'循环,ruby,for-loop,foreach,Ruby,For Loop,Foreach,以重写脚本的艰难方式学习Ruby: i = 0 numbers = [] while i < 6 puts "At the top i is #{i}" numbers.push(i) i += 1 puts "Numbers now: ", numbers puts "At the bottom i is #{i}" end puts "The numbers: " num
i = 0
numbers = []
while i < 6
puts "At the top i is #{i}"
numbers.push(i)
i += 1
puts "Numbers now: ", numbers
puts "At the bottom i is #{i}"
end
puts "The numbers: "
numbers.each {|num| puts num}
如何使用
每个
构造编写此脚本?您可以使用范围
对象和可枚举#每个
方法实现此目标:
(0...6).each do |i|
#some code here
end
您也可以使用
最多
0.高达(6){x}p“数字是{x}
6.次{x}数字您的描述是矛盾的。作者声称不对
使用,但要求您使用为
重写某些内容,而您得到的结果是作者告诉您避免的?
(0...6).each do |i|
#some code here
end