ruby array

Array.select方法 (Array.select Method)

In the last articles, we have seen how to iterate over the instances of Array class? We have seen that we have got methods like Array.each, Array.reverse_each and Array.map for this purpose. In this article, we will learn about the implementation of Array.select.

在上一篇文章中,我们已经看到了如何遍历Array类的实例? 我们已经看到,为此目的,我们已经有了Array.each , Array.reverse_each和Array.map之类的方法。 在本文中,我们将学习Array.select的实现。

Array.select method, as the name suggests, is used to select some elements from the Array. This method is non-destructive and does not bring any change in the actual values of the Array object. This method works based on certain conditions which you will provide inside the pair of parentheses. This method is based on the criteria you provide inside the block. This method will not work if you do not specify any conditions inside the block. Though it will not throw an exception you will get nil as the result. So, if you are working with this method you should be having certain conditions based on which the element is going to be selected from the object of Array class. If you want to print all the elements of Array instance then you can go for Array.each method and avoid going for this one.

顾名思义, Array.select方法用于从Array中选择一些元素。 此方法是非破坏性的,不会对Array对象的实际值带来任何变化。 该方法根据您将在圆括号内提供的某些条件来工作。 此方法基于您在块内提供的条件。 如果未在块内指定任何条件,则此方法将不起作用。 尽管它不会引发异常,但您将得到nil作为结果。 因此,如果使用此方法,则应具有某些条件,根据这些条件要从Array类的对象中选择元素。 如果要打印Array实例的所有元素,则可以使用Array.each方法,并避免使用该方法 。

Syntax:

句法:

    Array.select{|var| #condition}

Parameter(s):

参数:

This method does not permit the passing of any arguments instead it mandates a condition.

此方法不允许传递任何参数,而是要求一个条件。

Example 1:

范例1:

=begin
Ruby program to demonstrate Array.select
=end
# array declaration
num = [2,44,2,5,7,83,5,67,12,11,90,78,9]
puts "Enter 'a' for Even numbers and 'b' for odd numbers"
opt = gets.chomp
if opt == 'a'
puts "Even numbers are:"
puts num.select{|num|
num%2 == 0
}
elsif opt == 'b'
puts "Odd numbers are:"
puts num.select{|num|
num%2 !=0
}
else
puts "Wrong selection. Input valid option"
end

Output

输出量

RUN 1:
Enter 'a' for Even numbers and 'b' for odd numbers
a
Even numbers are:
2
44
2
12
90
78
RUN 2:
Enter 'a' for Even numbers and 'b' for odd numbers
b
Odd numbers are:
5
7
83
5
67
11
9

Explanation:

说明:

In the above code, you can observe that we are taking input from the user about what type of numbers the user wants as the output. This is because we want to pass certain conditions inside the Array.select method. We are giving the response to the user as per the option provided by the user and this method is used in this way only.

在上面的代码中,您可以观察到我们正在从用户那里获取用户想要输入什么类型的数字作为输入。 这是因为我们要在Array.select方法内部传递某些条件。 我们根据用户提供的选项向用户提供响应,并且仅以这种方式使用此方法。

Example 2:

范例2:

=begin
Ruby program to demonstrate Array.select
=end
# array declaration
num = [2,44,2,5,7,83,5,67,12,11,90,78,9]
puts num.select{|a|}

Output

输出量

    # no o/p

Explanation:

说明:

In the above output, you can observe that when you are not specifying any condition inside the method, then you are not getting anything as the output.

在上面的输出中,您可以观察到,当您未在方法内部指定任何条件时,则不会得到任何输出。

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

ruby array

ruby array_Ruby中带有示例的Array.select方法相关推荐

  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.keep_if方法

    ruby array Ruby Array.keep_if方法 (Ruby Array.keep_if Method) In the last articles, we have studied th ...

  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. 科大讯飞刷新纪录,机器阅读理解如何超越人类平均水平? | 技术头条
  2. Pig变飞机?AI为什么这么蠢 | Adversarial Attack
  3. vue vuex vue-router后台项目——权限路由(超详细简单版)
  4. java 的lambda表达式
  5. java graphics 类_对于 Graphics 类的一点认识(转)
  6. STL源码剖析 数值算法 copy 算法
  7. IDEA——必备插件指南
  8. 涨知识了!阿里、百度、腾讯的名字竟然是这样来的
  9. linux网络方面命令大全,Linux下必须知道的11个网络命令
  10. Windows11配置Java开发环境
  11. 《深入理解java虚拟机》 精华总结
  12. 深度剖析E680G应用二.MPKG
  13. 微信小程序引入 iconfont 图标
  14. 图解PLC与变频器通讯接线
  15. html里面输出大于号和小于号
  16. 拆弹实验-phase_7(隐藏关)
  17. JavaScript 之 学习网站推荐 强推【javascript.info】
  18. 关于安装Python过程中 无法访问Windows Installer服务问题解决
  19. Inner Join与Left Join
  20. 服务器虚拟地址是,两台服务器对外虚拟地址

热门文章

  1. html表单复选框样式,美化表单——自定义checkbox和radio样式
  2. 【WXS数据类型】Array
  3. line-height与图片底部间隙的学习整理转述
  4. Func 与Action
  5. fit_transform和transform的区别
  6. Ubuntu 16.04 下octave的使用入门
  7. pip3 install requests Cannot open D:\Python35\Scripts\pip3-script.py
  8. NOIP 2012 Day2
  9. oracle安装中桌面模式与服务器模式的去别
  10. webpack基础入门