ruby中的for in与each do的区别到底是什么?
PHP中文网
PHP中文网 2017-04-21 10:56:48
0
4
835

如果仅像某些文章说的前者的效率不如后者,那要前者做什么?

PHP中文网
PHP中文网

认证0级讲师

reply all(4)
Ty80

Under normal circumstances, try to use each:

1.upto(10).each { |i| puts i }
puts i # NameError: i is undefined

When using for:

for i in 1.upto(10) do
  puts i
end
puts i # print 10
阿神

It seems there is no difference, there are usually multiple ways to do one thing in ruby

刘奇

each do is more efficient. Some programming standards generally do not recommend using for in

洪涛

This is a matter of programming style. The each loop is a functional style, and the for loop is an imperative style
each is actually a method on an iterable object, accepting a block as a parameter
for is a loop structure implemented in the grammar

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template