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

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.eachArray.reverse_eachArray.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值