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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ruby array_Ruby中带有示例的Array.keep_if方法

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

ruby array

Ruby Array.keep_if方法 (Ruby Array.keep_if Method)

In the last articles, we have studied the Array methods namely Array.select, Array.reject and Array.drop_while, all these methods are non–destructive methods which means that they do not impose any changes in the actual values of elements residing in the Array instance. If you want to make the above methods destructive, you can add an “!” after the method name. For instance, Array.select! is the destructive version of Array.select. We have also learned about Array.delete_if the method which is already destructive by nature.

在上一篇文章中,我們研究了Array方法,即Array.select , Array.reject和Array.drop_while ,所有這些方法都是非破壞性的方法,這意味著它們不對駐留在數組中的元素的實際值施加任何更改。數組實例。 如果要使上述方法具有破壞性,則可以添加“!” 方法名稱之后。 例如,Array.select! 是Array.select的破壞性版本。 我們還了解了Array.delete_if該方法本質上已經具有破壞性。

In this article, we will learn about the Array method Array.keep_if which is also destructive by nature like the delete_if method.

在本文中,我們將學習Array方法Array.keep_if ,它與delete_if方法一樣也具有破壞性。

Method description:

方法說明:

This method is used to select elements from the instance of the Array class at the same instant. This method is just opposite of Array.delete_if method because Array.delete_if method deletes the elements from the Array which do not satisfy the condition provided inside the block on the other hand Array.keep_if method keeps or saves the elements which satisfy the condition specified inside the block of the method. This method will remove all the elements from the Array instance if you do not specify or provide any condition inside the block.

此方法用于同時從Array類的實例中選擇元素。 此方法與Array.delete_if方法恰好相反,因為Array.delete_if方法從Array中刪除不滿足塊內部提供的條件的元素,而Array.keep_if方法則保留或保存滿足內部指定條件的元素方法的塊。 如果您未在塊內指定或提供任何條件,則此方法將從Array實例中刪除所有元素。

Syntax:

句法:

Array.keep_if{|var|#condition}

Parameter(s): This method does not accept any arguments instead it requires a Boolean condition for operation.

參數 :此方法不接受任何參數,而是需要布爾條件進行操作。

Example 1:

范例1:

=beginRuby program to demonstrate Array.keep_if =end# array declaration num = [1,2,3,4,5,6,7,8,9,10,23,11,33,55,66,12]# user input puts "Enter the your choice (a)keep even numbers (b) keep odd numbers" lm = gets.chompif lm == 'a'puts "Even numbers are:"print num.keep_if { |a| a % 2 ==0 } elsif lm == 'b'puts "Odd numbers are:"print num.keep_if { |a| a % 2 !=0 } elseputs "Invalid Input" end

Output

輸出量

RUN 1: Enter the your choice (a)keep even numbers (b) keep odd numbers a Even numbers are: [2, 4, 6, 8, 10, 66, 12]RUN 2: Enter the your choice (a)keep even numbers (b) keep odd numbers b Odd numbers are: [1, 3, 5, 7, 9, 23, 11, 33, 55]

Explanation:

說明:

In the above code, you can observe that the method is keeping all the elements that are satisfying the condition provided inside the block of Array.keep_if method. If the user is asking to keep odd numbers, then output is shown in RUN 2 and when the user is asking for even numbers, the output is shown in RUN 1.

在上面的代碼中,您可以觀察到該方法將保留所有滿足Array.keep_if方法塊中提供的條件的元素。 如果用戶要求保留奇數,則在RUN 2中顯示輸出,而當用戶要求偶數時,在RUN 1中顯示輸出。

Example 2:

范例2:

=beginRuby program to demonstrate Array.keep_if =end# array declaration num = [1,2,3,4,5,6,7,8,9,10,23,11,33,55,66,12]print num.keep_if{|a|} puts "" print num

Output

輸出量

[] []

Explanation:

說明:

In the above code, you can observe that if you are not specifying any condition inside the method block, then it is deleting or removing all the elements from the Array instance.

在上面的代碼中,您可以觀察到,如果您未在方法塊內指定任何條件,那么它將在Array實例中刪除或刪除所有元素。

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

ruby array

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。