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:

=begin
Ruby 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.chomp
if 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 }
else
puts "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:

=begin
Ruby 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方法相关推荐

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

    ruby array Array.shuffle方法 (Array.shuffle Method) In this article, we will study about Array.shuffle ...

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

    ruby array Array.select方法 (Array.select Method) In the last articles, we have seen how to iterate ov ...

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

    ruby array Ruby Array.delete_if方法 (Ruby Array.delete_if Method) In the last articles, we have studie ...

  4. ruby array_Ruby中带有示例的Array.cycle()方法

    ruby array Array.cycle()方法 (Array.cycle() Method) In this article, we will study about Array.cycle() ...

  5. ruby array_Ruby中带有示例的Array.fill()方法(1)

    ruby array Array.fill()方法 (Array.fill() Method) In this article, we will study about Array.fill() me ...

  6. ruby array_Ruby中带有示例的Array.index()方法

    ruby array Array.index()方法 (Array.index() Method) In this article, we will study about Array.index() ...

  7. ruby array_Ruby中带有示例的Array.sample()方法

    ruby array Array.sample()方法 (Array.sample() Method) In this article, we will study about Array.sampl ...

  8. ruby array_Ruby中带有示例的Array.fill()方法(3)

    ruby array Array.fill()方法 (Array.fill() Method) In this article, we will study about Array.fill() me ...

  9. ruby array_Ruby中带有示例的Array.zip()方法

    ruby array Array.zip()方法 (Array.zip() Method) In this article, we will study about Array.zip() Metho ...

最新文章

  1. C#中如何将将数据导出到word excel 中
  2. 20145129 《Java程序设计》第6周学习总结
  3. openocd调试Linux内核,Ubuntu下配置OpenOCD+FT2232
  4. 5.TCP和UDP的区别
  5. 数据有了,如何构建数据资产?
  6. python 项目自动生成requirements.txt文件
  7. flight php 中文,PHP: composer的简单使用
  8. contourf参数 python_Python机器学习(六)
  9. 《Deep Learning》—— 数学基础
  10. Java使用Openimaj构建视觉词袋模型
  11. win32常用文件操作
  12. 数据库: asc和desc的意思
  13. 一目了然,看民生银行 IT 运维故障管理可视化案例
  14. 从零开始学编程_数据结构
  15. 微博营销的优劣势。如何利用微博开展营销。
  16. findfont: Font family [‘sans-serif‘] not found解决方法
  17. 数据结构系列-队列的基本操作
  18. 语言模型数据集 (周杰伦专辑歌词)
  19. 围城书评_书评:关于HTML5的真相
  20. windows平台下静态库(.lib)和动态库(.dll)使用

热门文章

  1. Nexus搭建Maven私有仓库
  2. Angular之ngx-permissions安装入门
  3. javascript复制到黏贴板之完美兼容
  4. C#提取文件名【C#】
  5. 【manacher】Strings in the Pocket
  6. kafka 支持发布订阅
  7. redhat linux配置RSH遇见的问题
  8. Exchange 2003升级至Exchange 2007
  9. chrome解决跨域(CORS)问题---chrome插件
  10. Hive的使用之hwi