ruby array_Array.select! Ruby中的示例方法

ruby array

Array.select! 方法 (Array.select! Method)

In this article, we will study about Array.select! Method. You all must be thinking the method must be doing something related to the selection of objects from the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

在本文中,我们将研究Array.select! 方法 。 你们都必须认为该方法必须在做一些与从Array实例中选择对象有关的事情。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

Method description:

方法说明:

This method is a public instance method and defined for the Array class in Ruby's library. This method, as the name suggests, is used to select some elements from the Array. This method is destructive and brings changes in the actual values of the Array object. This method works based on a certain condition which you will provide inside the pair of parenthesis. This method is based on the criteria you provide inside the block. This method will not work if you do not specify any condition 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 a certain condition 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.

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

Syntax:

句法:

    array.select!{|var| #condition}

Argument(s) required:

所需参数:

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! method
=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
	}
	puts "Array instance: #{num}"
elsif opt == 'b'
	puts "Odd numbers are:"
	puts num.select!{|num|
		num%2 !=0
	}
	puts "Array instance: #{num}"
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
Array instance: [2, 44, 2, 12, 90, 78]

RUN 2:
nter 'a' for Even numbers and 'b' for odd numbers
 b
Odd numbers are:
5
7
83
5
67
11
9
Array instance: [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 responses to the user as per the option provided by the user and this method is used in this way only. You can see that this method is creating a direct impact on the self Array instance because this method is one of the examples if destructive methods.

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

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 Output.

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-inverse-method-with-example.aspx

ruby array

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值