Syntax for a for loop in ruby

前端 未结 10 1052
梦如初夏
梦如初夏 2020-12-02 07:20

How do I do this type of for loop in Ruby?

for(int i=0; i
10条回答
  •  再見小時候
    2020-12-02 07:35

    Ruby's enumeration loop syntax is different:

    collection.each do |item|
    ...
    end
    

    This reads as "a call to the 'each' method of the array object instance 'collection' that takes block with 'blockargument' as argument". The block syntax in Ruby is 'do ... end' or '{ ... }' for single line statements.

    The block argument '|item|' is optional but if provided, the first argument automatically represents the looped enumerated item.

提交回复
热议问题