{/hx}那個……
for a in b
end
和
b.each {|a|}
有甚麽區別?
以前看過,但是忘記了。
網上找到這個說明,
補充一下:
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
那麽說,是否意味
for a in b的速度比b.each {|a|}慢?
而消耗的內存比b.each {|a|}?