11.Earlier we said that the only built-in Ruby looping primitives were while and until. What's this ``for'' thing, then? Well, for is almost a lump of syntactic sugar.
在Ruby中,内置的循环语句只有while和until(具体使用请参考文档).普天下的程序员熟悉的for呢? 在Ruby中,for其实仅仅是一个语法糖而已,使用for循环的时候,Ruby会将for翻译为调用集合对象的each方法,所以,下面两个定义是等同的:
for aSong in songList
aSong.play
end
songList.each do |aSong|
aSong.play
end
欢迎光临 Project1 (https://rpg.blue/) | Powered by Discuz! X3.1 |