ruby中、.reject_Array.reject! Ruby中的示例方法

ruby中、.reject

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

In this article, we will study about Array.reject! 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.reject! 方法 。 你们都必须认为该方法必须在做一些与从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 reject some elements from the Array. The elements will not be printed if they are satisfying the condition or criteria provided inside the block. 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 work even if you do not specify any condition inside the block. It will print all the values if you are not providing any condition or criteria of rejection.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 顾名思义,此方法用于拒绝Array中的某些元素 。 如果元素满足块内提供的条件或标准,则不会打印这些元素。 此方法具有破坏性,并会更改Array对象的实际值。 此方法基于您将在圆括号内提供的特定条件。 此方法基于您在块内提供的条件。 即使您未在块内指定任何条件,此方法也将起作用。 如果您未提供任何拒绝条件或条件,它将打印所有值。

Syntax:

句法:

    array.reject!{|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.reject!
=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.reject!{|num|
		num%2 != 0
	}
	puts "Array instance: #{num}"
elsif opt == 'b'
	puts "Odd numbers are:"
	puts num.reject!{|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:
Enter '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.reject! 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. It is rejecting the elements which are satisfying the condition provided inside the block. You can see that this method is creating a direct impact on the self Array instance because this method is one of the examples of destructive methods.

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

Example 2:

范例2:

=begin
  Ruby program to demonstrate Array.reject!
=end

# array declaration
num = [2,44,2,5,7,83,5,67,12,11,90,78,9]

puts num.reject!{|a|}

Output

输出量

No Output.

Explanation:

说明:

In the above output, you can observe that when you are not specifying any condition inside the method, it is still not throwing any kind of exception inside it will print all the elements present in the Array instance. This is one more point by which we can differentiate Array.select! and Array.reject!.

在上面的输出中,您可以观察到,当您在方法内未指定任何条件时,它仍未引发任何异常,它将打印Array实例中存在的所有元素。 这是我们可以区分Array.select的另一点Array.reject!

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

ruby中、.reject

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值