我遇到了三种编写循环的方法.
the_count = [1, 2, 3, 4, 5]
-
for循环1
for number in the_count puts "This is count #{number}" end -
for循环2
the_count.each do |count| puts "The counter is at: #{count}" end -
for循环3
the_count.each {|i| puts "I got #{i}"}
在某些情况下,有一种方法是比其他两种方法更好的实践或更好的解决方案吗?第一个与其他语言的最相似,对我来说,第三个看起来无序.
解决方案通常不建议使用第一种方法.在ruby中,可能对来自其他语言的开发人员更加友好(因为他们可以识别语法),但是对于变量可见性而言,它的行为有些奇怪.通常,您应该在任何地方都避免使用此变体,而只能使用一个块变体.
其他两个变体的优点是,对于接受块的所有方法,例如,它的工作方式都是相同的. map,reduce,take_while等.
两个底部变体在大多数情况下是等效的您可以使用each方法并为其提供一个块. each方法为数组中的每个元素调用一次该块.
您使用的哪一个主要取决于偏好.大多数人倾向于将带有括号的对象用于不需要换行的简单块.如果您想在区块中使用换行符,例如如果在那里有多个语句,则应使用do ... end变体.这使您的代码更具可读性.
关于何时应该使用另一个的细微差别(例如,有些人在编写功能块时总是使用花括号形式,即即使更长的时间也不会影响块的外部) ,但如果您遵循上述建议,则至少请98%的红宝石开发人员阅读您的代码.
因此,总而言之,请避免使用for i in ...变体(while,until,...的计数相同),并始终使用块形式.对于复杂的块,使用do ... end块,对于简单的单行块,使用大括号形式.
使用块形式时,应注意链接方法时优先级的细微差别.
此
foo bar { |i| puts i }等同于
foo(bar{|i| puts i})同时
foo bar do |i| puts i end等同于
foo(bar) { |i| puts i }如您所见,以大括号形式将块传递到最右边的方法,而以do ... end形式将块传递给最左边的方法.不过,您始终可以使用括号解决歧义.
I came across three ways of writing a loop.
the_count = [1, 2, 3, 4, 5]
for loop 1
for number in the_count puts "This is count #{number}" endfor loop 2
the_count.each do |count| puts "The counter is at: #{count}" endfor loop 3
the_count.each {|i| puts "I got #{i}"}
Are there situations in which one way is a good practice or better solution than the other two? The first one is the most similar to the ones in other languages, and for me, the third one looks unorderly.
解决方案The first option is generally discouraged. It is possible in ruby to be more friendly towards developers coming from other languages (as they recognize the syntax) but it behaves a bit strange regarding variable visibility. Generally, you should avoid this variant everywhere and use only one of the block variants.
The advantage of the two other variants is that it works the same for all methods accepting a block, e.g. map, reduce, take_while and others.
The two bottom variants are mostly equivalent You use the each method and provide it with a block. The each method calls the block once for each element in the array.
Which one you use is mostly up to preference. Most people tend to use the one with braces for simple blocks which don't require a line-break. If you want to use a line-break in your block, e.g. if you have multiple statements there, you should use the do...end variant. This makes your code more readable.
There are other slightly more nuanced opinions on when you should use one or the other (e.g. some always use the braces form when writing functional block, i.e. ones which don't affect the outside of the block even when they are longer), but if you follow this above advice, you will please at least 98% of all ruby developers reading your code.
Thus, in conclusion, avoid the for i in ... variants (the same counts for while, until, ...) and always use the block-form. Use the do...end of block for complex blocks and the braces-form for simple one-line blocks.
When you use the the block form, you should however be aware of the slight differences in priority when chaining methods.
This
foo bar { |i| puts i }is equivalent to
foo(bar{|i| puts i})while
foo bar do |i| puts i endis equivalent to
foo(bar) { |i| puts i }As you can see, in the braces form, the block is passed to the right-most method while in the do...end form, the block is passed to the left-most method. You can always resolve the ambiguity with parenthesis though.
更多推荐
Ruby for循环的最佳做法
发布评论