assoc.rsp_Ruby中带有示例的Array.assoc(obj)方法

assoc.rsp

Ruby Array.assoc(obj)方法 (Ruby Array.assoc(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 this article, we will see how we can implement the Array.assoc() method? We will go through its syntax and some examples in the rest of the Array.

在前面的文章中,我们已经看到如何借助<=>运算符==运算符和.eql来检查两个Array实例是否相同 。 方法? 我们还看到了可以在先前定义的Array实例中插入元素的不同方法。 现在,可以说我们已经对Ruby语言中的Array类有了相当多的了解。 在本文中,我们将看到如何实现Array.assoc()方法 ? 我们将在数组的其余部分中介绍其语法和一些示例。

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. Let us go through the syntax and demonstrating the program codes of this method.

该方法是Public实例方法,属于Array类,该类位于Ruby语言库中。 此方法用于检查对象是否为特定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.assoc(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 assoc 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.assoc(ele) != nil
	puts "Element found in:"
	print  arraymain.assoc(ele)
else
	puts "Element not found"
end

Output

输出量

RUN 1:
Enter the element you want to search
Orange
Element found in:
["Orange","Banana","Papaya","Apricot","Grapes"]

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

Explanation:

说明:

In the above code, you can find that the Array instance on which we have invoked assoc() 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.

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

Example 2:

范例2:

=begin
  Ruby program to demonstrate assoc method
=end

# array
array1 = ["Babita","Sabita","Ashok"]

# checking 
puts array1.assoc("Babita")

Output

输出量

None

Explanation:

说明:

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

在上面,您可以验证assoc()方法对常规Array实例不起作用。 即使对象是Array实例的一部分,它也将返回nil。

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

assoc.rsp

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值