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" endOutput
輸出量
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 numOutput
輸出量
[] []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方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 清除元素中的子元素html_HTML中的
- 下一篇: GMTA的完整形式是什么?