ruby array_Ruby中带有示例的Array.rassoc()方法

ruby array

Array.rassoc()方法 (Array.rassoc() Method)

In this article, we will study about Array.rassoc() method. You all must be thinking the method must be doing something which is quite different from all those methods which we have studied. 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.rassoc()方法 。 大家都必须认为该方法必须做的事情与我们研究的所有这些方法截然不同。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

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 and that Array instance cannot be a normal Array instance. If it is not normal, it means that Array instance is the Array of multiple Array instances or you can say that it the collection of multiple objects which are itself an object of Array class. It works for the Array instances whose elements are also Array instances. Let us go through the syntax and demonstrating the program codes of this method.

该方法是Public实例方法,属于Array类,该类位于Ruby语言库中。 此方法用于检查对象是否为特定Array实例的一部分,并且该Array实例不能为普通Array实例。 如果不正常,则表示Array实例是多个Array实例的Array,或者可以说它是多个对象的集合,而这些对象本身就是Array类的对象。 它适用于其元素也是Array实例的Array实例。 让我们来看一下语法,并演示该方法的程序代码。

If you are thinking about what it will return then let me tell you, it will return the first contained Array instance where it found the presence of the object. It will return "nil" if it hadn't found the object in any of the Arrays.

如果您正在考虑它将返回什么,那么让我告诉您,它将返回找到对象存在的第一个包含的Array实例。 如果未在任何数组中找到对象,它将返回“ nil”。

Syntax:

句法:

    array_instance.rassoc(obj)

Argument(s) required:

所需参数:

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 rassoc method
=end

# array declarations
array1 = [1,"one"]
array2 = [2,"two"]
array3 = [3,"three"]

array_main = [array1,array2,array3]

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

if array_main.rassoc(ele) != nil
	puts "Element found in:"
	print  array_main.rassoc(ele)
else
	puts "Element not found"
end

Output

输出量

RUN 1:
Enter the element you want to search
 two
Element found in:
[2, "two"]

RUN 2:
Enter the element you want to search
 one
Element found in:
[1, "one"]

Explanation:

说明:

In the above code, you can find that the Array instance on which we have invoked rassoc() method is not any normal Array instance. It is the collection of multiple Array instances. It is returning the whole Array instance where it has found the object inputted by the user.

在上面的代码中,您可以发现我们调用rassoc()方法的 Array实例不是任何普通的Array实例。 它是多个Array实例的集合。 它返回找到用户输入对象的整个Array实例。

Example 2:

范例2:

=begin
  Ruby program to demonstrate rassoc method
=end

# array declaration
array1 = ["Babita","Sabita","Ashok"]
puts array1.rassoc("Babita")

Output

输出量

No output

Explanation:

说明:

In the above you can verify that rassoc() method does not works upon normal Array instances. It will return nil even if the object is a part of the Array instance.

在上面,您可以验证rassoc()方法不适用于普通Array实例。 即使对象是Array实例的一部分,它也将返回nil。

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

ruby array

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值