ruby .each_Ruby中带有示例的Array.each方法
ruby .each
Ruby Array.each方法 (Ruby Array.each method)
Array.each method can be easily termed as a method which helps you to iterate over the Array. This method first processes the first element then goes on the second and the process keeps on going on until the last element is not processed. This method is very important when it comes to carrying out the process of traversing the array. This method works in the way that each element of the instance of Array class is yielded to the block supplied to the method. The task is completed in a particular sequence. You will need to provide a temporary variable inside the pair of the disjoint symbol because you will be able to fetch the element with the help of that variable only. This simply means that first the value of an element is temporarily stored in that variable and it is stored ever since the next element does not come into the scene.
Array.each方法可以輕松地稱為可幫助您遍歷Array的方法 。 此方法首先處理第一個元素,然后繼續(xù)處理第二個元素,然后繼續(xù)進行直到?jīng)]有處理最后一個元素。 在執(zhí)行遍歷數(shù)組的過程時,此方法非常重要。 該方法的工作方式是將Array類實例的每個元素都屈服到提供給該方法的塊中。 該任務(wù)按特定順序完成。 您將需要在不相交符號對中提供一個臨時變量,因為您將只能在該變量的幫助下獲取元素。 這僅意味著首先將元素的值臨時存儲在該變量中,并且由于下一個元素不進入場景就將其存儲。
This operation or process does not bring any change in the actual values of Array elements.
此操作或過程不會使Array元素的實際值發(fā)生任何變化。
Syntax:
句法:
Array.each { |var| #statements}Parameter(s):
參數(shù):
This method does not invite any type of argument.
此方法不邀請任何類型的參數(shù)。
Example 1:
范例1:
=begin Ruby program to demonstrate Array.each =end# array declaration Adc = ['Ruby','Includehelp.com','Ruby','C++','C#']# counter intialization cnt = 1# Array.each method Adc.each{ |ele|puts "#{cnt} element is #{ele}"cnt = cnt + 1 }Output
輸出量
1 element is Ruby 2 element is Includehelp.com 3 element is Ruby 4 element is C++ 5 element is C#The above code can be modified as,
上面的代碼可以修改為
Example 2:
范例2:
=begin Ruby program to demonstrate Array.each =end# array declaration Adc = ['Ruby','Includehelp.com','Ruby','C++','C#']# counter initialization cnt = 1 Adc.each do |ele|puts "#{cnt} element is #{ele}"cnt = cnt + 1 endOutput
輸出量
1 element is Ruby 2 element is Includehelp.com 3 element is Ruby 4 element is C++ 5 element is C#Explanation:
說明:
In the above two program codes, you can observe that the method named Array.each can be used in two different ways. Both the ways are quite understandable and their use depends upon your comfort. It is very clear with the help of output that the processing of Array elements starts from index 0 and finishes when the last element is being processed. You can never bring any change in the elements of Array with the help of this method because this method is one of the examples of non-destructive methods.
在以上兩個程序代碼中,您可以觀察到名為Array.each的方法可以以兩種不同的方式使用。 兩種方式都是可以理解的,它們的使用取決于您的舒適度。 在輸出的幫助下,很明顯Array元素的處理從索引0開始,并在處理最后一個元素時結(jié)束。 借助于此方法,您永遠無法對Array的元素進行任何更改,因為該方法是非破壞性方法的示例之一。
翻譯自: https://www.includehelp.com/ruby/array-each-method-with-example.aspx
ruby .each
總結(jié)
以上是生活随笔為你收集整理的ruby .each_Ruby中带有示例的Array.each方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python公共变量_Python中的公
- 下一篇: java range类_Java即时类|