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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

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

ruby array

Ruby Array.delete_if方法 (Ruby Array.delete_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 method destructive, you can add an "!" after the method name. For instance, Array.select! is the destructive version of Array.select.

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

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

在本文中,我們將學習Array方法Array.delete_if ,該方法本質上已經具有破壞性。

Method description:

方法說明:

The changes created by this method are always permanent as it is one of a kind of destructive methods. This method works in a way that if it finds an element which is not satisfying the Boolean condition which is specified inside the block of the method, then it deletes that element from the Array instance. It will not delete any element if it does not find the Boolean condition specified inside the block of the method.

通過這種方法創建的更改始終是永久性的,因為它是一種破壞性方法。 此方法的工作方式是,如果找到不滿足該方法塊內指定的布爾條件的元素,則將其從Array實例中刪除。 如果找不到方法塊內指定的布爾條件,它將不會刪除任何元素。

Syntax:

句法:

Array.delete_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.delete_if =end# array declaration num = [1,2,3,4,5,6,7,8,9,10,23,11,33,55,66,12]# input puts "Enter the your choice (a)delete odd numbers (b) delete even numbers" lm = gets.chompif lm == 'a'puts "Even numbers are:"puts num.delete_if { |a| a % 2 !=0 } elsif lm == 'b'puts "Odd numbers are:"puts num.delete_if { |a| a % 2 ==0 } elseputs "Invalid Input" end

Output

輸出量

RUN 1: Enter the your choice (a)delete odd numbers (b) delete even numbers a Even numbers are: 2 4 6 8 10 66 12RUN 2: Enter the your choice (a)delete odd numbers (b) delete even 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 deleting all the elements which are satisfying the condition provided inside the block of Array.delete_if method. If the user is asking to delete odd numbers, then output is shown in RUN2 and when the user is asking for even numbers, the output is shown in the RUN1.

在上面的代碼中,您可以觀察到該方法正在刪除所有滿足Array.delete_if method塊中提供的條件的元素。 如果用戶要求刪除奇數,則在RUN2中顯示輸出,而當用戶要求偶數時,在RUN1中顯示輸出。

Example 2:

范例2:

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

Output

輸出量

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 23, 11, 33, 55, 66, 12] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 23, 11, 33, 55, 66, 12]

Explanation:

說明:

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

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

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

ruby array

總結

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

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