日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ruby .each_Ruby中带有示例的Array.each方法

發(fā)布時間:2025/3/11 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby .each_Ruby中带有示例的Array.each方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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ìn)行直到?jīng)]有處理最后一個元素。 在執(zhí)行遍歷數(shù)組的過程時,此方法非常重要。 該方法的工作方式是將Array類實(shí)例的每個元素都屈服到提供給該方法的塊中。 該任務(wù)按特定順序完成。 您將需要在不相交符號對中提供一個臨時變量,因?yàn)槟鷮⒅荒茉谠撟兞康膸椭芦@取元素。 這僅意味著首先將元素的值臨時存儲在該變量中,并且由于下一個元素不進(jìn)入場景就將其存儲。

This operation or process does not bring any change in the actual values of Array elements.

此操作或過程不會使Array元素的實(shí)際值發(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 end

Output

輸出量

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é)束。 借助于此方法,您永遠(yuǎn)無法對Array的元素進(jìn)行任何更改,因?yàn)樵摲椒ㄊ欠瞧茐男苑椒ǖ氖纠弧?

翻譯自: https://www.includehelp.com/ruby/array-each-method-with-example.aspx

ruby .each

總結(jié)

以上是生活随笔為你收集整理的ruby .each_Ruby中带有示例的Array.each方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。