obj[]与obj._Ruby中带有示例的Array.include?(obj)方法

obj[]与obj.

Ruby Array.include?(obj)方法 (Ruby Array.include?(obj) Method)

In the previous articles, we have seen how we can check whether two Array instances are identical or not with the help of <=> operator, == operator, and .eql? method? We have also seen different ways through which we can insert elements in the previously defined Array instances. Now, we can say that we have got a decent amount of knowledge about the Array class in Ruby language. In the last article, we have seen the implementation of the assoc() method but the assoc method only works for the Array object which is the collection of multiple Array objects.

在前面的文章中,我们已经看到如何借助<=>运算符==运算符.eql检查两个Array实例是否相同 方法 ? 我们还看到了可以在先前定义的Array实例中插入元素的不同方法。 现在,可以说我们已经对Ruby语言中的Array类有了相当多的了解。 在上一篇文章中,我们看到了assoc()方法的实现,但是assoc方法仅适用于Array对象,后者是多个Array对象的集合。

For normal Array objects, we have Array.include?(obj) method. In this article, we will see how we can implement Array.include?() method? We will go through its syntax and some examples in the rest of the Array.

对于普通的Array对象,我们有Array.include?(obj)方法 。 在本文中,我们将看到如何实现Array.include?()方法 ? 我们将在数组的其余部分中介绍其语法和一些示例。

Method description:

方法说明:

This method is a Public instance method and belongs to the Array class which lives inside the library of Ruby language. This method is used to check whether an object is a part of the particular Array instance or not. It will search through the whole Array and gives you the result according to its search. Let us go through the syntax and demonstrating the program codes of this method.

该方法是Public实例方法,属于Array类,该类位于Ruby语言库中。 此方法用于检查对象是否为特定Array实例的一部分。 它将搜索整个数组,并根据其搜索结果。 让我们来看一下语法,并演示该方法的程序代码。

If you are thinking about what it will return then let me tell you, it will return a Boolean value. The returned value will be true if it finds the object inside the Array and the return value will be false if it does not find the object to be the part of the Array instance.

如果您正在考虑它将返回什么,那么让我告诉您,它将返回一个布尔值。 如果在数组内找到对象,则返回值将为true;如果找不到对象是Array实例的一部分,则返回值为false。

Syntax:

句法:

    array_instance.include?(obj)

Parameter(s):

参数:

This method only takes one parameter and that argument is nothing but an object whose presence we want to check.

此方法仅使用一个参数,而该参数不过是一个要检查其存在性的对象。

Example 1:

范例1:

=begin
  Ruby program to demonstrate include method
=end

# array
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]

# input element to search
puts "Enter the element you want to search"
ele = gets.chomp

# checking
if array1.include?(ele) != false
	puts "Element found"
else
	puts "Element not found"
end

Output

输出量

RUN 1:
Enter the element you want to search
 Apple
Element found

RUN 2:
Enter the element you want to search
 Mango
Element not found

Explanation:

说明:

In the above code, you can observe that we are invoking the include method on the normal Array instance. It has returned true when it found the presence of an object in the Array object which is entered by the user.

在上面的代码中,您可以观察到我们在普通Array实例上调用include方法。 当发现用户输入的Array对象中存在对象时,它返回true。

Example 2:

范例2:

=begin
  Ruby program to demonstrate include method
=end

# arrays
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]
array2 = ["Akul","Madhu","Ashok","Mukesh",788]
array3 = ["Orange","Banana","Papaya","Apricot","Grapes"]

# main array
arraymain = [array1,array2,array3]

# input element to search
puts "Enter the element you want to search"
ele = gets.chomp

# checking
if arraymain.include?(ele) != false
	puts "Element found"
else
	puts "Element not found"
end

Output

输出量

Enter the element you want to search
 Array1
Element not found

Explanation:

说明:

In the above, you can verify that include method does not work upon Array instance which is the collection of multiple Array instances. It will return false even if the object is a part of the Array instance.

在上面,您可以验证include方法不适用于Array实例,该实例是多​​个Array实例的集合。 即使对象是Array实例的一部分,它也会返回false。

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

obj[]与obj.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值