assoc_Ruby assoc()函数

assoc

Ruby中的assoc()函数 (assoc() function in Ruby)

We have studied functions to process single dimensional array so far but if we talk about assoc() function, it does not work for single dimensional arrays. assoc() function only works on Array of Arrays or you can say that it works on multidimensional arrays. The purpose of the assoc() function is to check the first element of each array. It processes array by checking the first element of the array with the specified index element, if the element is found then the whole array is returned otherwise the function returns nil or vacant.

到目前为止,我们已经研究了处理一维数组的函数,但是如果我们谈论assoc()函数 ,则该函数不适用于一维数组。 assoc()函数仅适用于数组数组,或者可以说它适用于多维数组。 assoc()函数的目的是检查每个数组的第一个元素。 它通过使用指定的索引元素检查数组的第一个元素来处理数组,如果找到该元素,则返回整个数组,否则该函数返回nil或vacant。

Syntax:

句法:

 Array_name.assoc(Object)

Now, let us understand the implementation of assoc() function with the help of examples broadly,

现在,让我们借助示例广泛地理解assoc()函数的实现,

Example 1:

范例1:

=begin
Ruby program to demonstrate implementation of assoc() function
=end
# Initializing multiple arrays of elements 
Arr1 = ["Fruits", "banana", "apple", "orange", "kiwi", "apricot", "Pineapple"] 
Arr2 = ["Languages", "C#", "Java", "Ruby", "Python", "C++","C"] 
Arr3 = ["Colors", "Red", "Brown", "Green", "Pink", "Yellow", "Teal"]
Arr4 = ["Vegetables", "Brinjal", "Tomato", "Potato", "Reddish"] 
# Creating a final array of above arrays 
FinalArray = [Arr1, Arr2, Arr3, Arr4] 
# Invoking assoc() function 
A1 = FinalArray.assoc("Fruits") 
A2 = FinalArray.assoc("Languages") 
A3 = FinalArray.assoc("Colors")
A4 = FinalArray.assoc("Vegetables") 
# Printing the matched contained array 
puts "#{A1}"
puts "#{A2}"
puts "#{A3}"
puts "#{A4}"

Output

输出量

["Fruits", "banana", "apple", "orange", "kiwi", "apricot", "Pineapple"]
["Languages", "C#", "Java", "Ruby", "Python", "C++", "C"]
["Colors", "Red", "Brown", "Green", "Pink", "Yellow", "Teal"]
["Vegetables", "Brinjal", "Tomato", "Potato", "Reddish"]

Code logic:

代码逻辑:

In the above example, we have initialized four arrays along with their categories as the first element of the array. Finalarray is storing these arrays as its elements. When the assoc() function is invoked with the element as an index, then the element is compared with the first element of each array. Arrays A1, A2, A3, and A4 are storing the contained array.

在上面的示例中,我们初始化了四个数组及其类别,作为数组的第一个元素。 Finalarray将这些数组作为元素存储。 当使用该元素作为索引调用assoc()函数时 ,该元素将与每个数组的第一个元素进行比较。 数组A1,A2,A3和A4正在存储包含的数组。

Example 2:

范例2:

=begin
Ruby program to demonstrate implementation of assoc() function
=end
# Initializing multiple arrays of elements 
Arr1 = ["Fruits", "banana", "apple", "orange", "kiwi", "apricot","Pineapple"] 
Arr2 = ["Languages", "C#", "Java", "Ruby", "Python","C++","C"] 
Arr3 = ["Colors", "Red", "Brown", "Green", "Pink","Yellow","Teal"]
Arr4 = ["Vegetables", "Brinjal", "Tomato", "Potato", "Raddish"] 
# Creating a final array of above arrays 
FinalArray = [Arr1, Arr2, Arr3, Arr4] 
# Taking input from user.
puts "Enter the catagory:-"
cat = gets.chomp
arr = FinalArray.assoc(cat)
if (arr==nil)
puts "category not available"
else
puts "The available elements under the category is:-"
puts "#{arr}"
end

Output

输出量

Run 1:-
Enter the category:-
Colors
The available elements under the category is:-
["Colors", "Red", "Brown", "Green", "Pink", "Yellow", "Teal"]
Run 2:-
Enter the category:-
Cars
category not available
Run 3:-
Enter the category:-
Fruits
The available elements under the category is:-
["Fruits", "banana", "apple", "orange", "kiwi", "apricot", "Pineapple"]

Code logic:

代码逻辑:

In this example, we have declared four arrays and then the final array. We are taking category as input from the user. We are passing that category to the assoc() function. If the category matches, the array is stored in the contained array otherwise assoc() is returning nil.

在此示例中,我们声明了四个数组,然后声明了最后一个数组。 我们将类别作为用户的输入。 我们将该类别传递给assoc()函数 。 如果类别匹配,则将数组存储在包含的数组中,否则assoc()返回nil。

翻译自: https://www.includehelp.com/ruby/assoc-function.aspx

assoc

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543744.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端接入HTTP协议浅析

【摘要】:本文整理并简要分析了HTTP协议的交互过程和内容格式,包括HTTP请求、HTTP应答的头域和实体内容,HTTP 1.0与HTTP 1.1的差异,并举例说明了Chunked编码的工作过程原理。1、HTTP协议简介浏览器和Web服务器之间一问一答的交互过…

互联网传真 传真指令_传真的完整形式是什么?

互联网传真 传真指令传真:传真 (FAX: Facsimile) FAX is an abbreviation of "Facsimile". 传真是“传真”的缩写 。 It is commonly written and spoken as FAX. It is a telephonic transmission of a scanned copy of text and images printed on a p…

C#使用七牛云存储上传下载文件、自定义回调

项目需要将音视频文件上传服务器,考虑并发要求高,通过七牛来实现。 做了一个简易的压力测试,同时上传多个文件,七牛自己应该有队列处理并发请求,我无论同时提交多少个文件,七牛是批量一个个排队处理了。 一…

netfilter que_QUE的完整形式是什么?

netfilter que问题:问题 (QUE: Questions) QUE is an abbreviation of "Questions". QUE是“ Questions”的缩写 。 It is an expression, which is commonly used in the Gmail platform. It is written in the body or the subject of the email to te…

mysqld_multi stop 不能停掉mysql

使用mysqld_multi start 启动了多个mysql,但是mysqld_multi stop 却不能停止原因:因为你还没有授权/usr/local/mysql/bin/mysqld_multi stop 但是默认是停不掉的,需要我们做一个授权grant shutdown on *.* to usernamelocalhost identified…

热冗余冷冗余_冗余支架

热冗余冷冗余Problem statement: 问题陈述: Given a string of balanced expression, find if it contains a redundant parenthesis or not. A set of parentheses is redundant if the same sub-expression is surrounded by unnecessary or multiple brackets. …

对称树

Problem statement: 问题陈述: Given a binary Tree, check whether the tree is symmetric or not. 给定二叉树 , 检查树是否对称 。 Input Example: 输入示例: For example1/ \2 2/ \ / \3 4 4 3The above tree is symmetric1/ \2 …

unity, undo

如果在操作一个Object之前调用Undo.RecordObject(Object),且操作确实造成Object某些属性的改变,则会产生一个undo记录。 如果我们的架构不是直接操作Object,而是操作一个ui,并在某些时机通过ui.save(Object)将数据回写到Object&am…

ajax应用_AJAX的应用

ajax应用AJAX has several benefits that can be utilized inside a web application. In this article, well explore some advantages of AJAX and see some of its applications. AJAX具有可在Web应用程序内部使用的多个优点。 在本文中,我们将探讨AJAX的一些优势…

Linux下DRBD配置

一、什么是DRBD1、简介 Distributed Replicated Block Device(DRBD)是一个用软件实现的、无共享的、服务器之间镜像块设备内容的存储复制解决方案。数据镜像:实时、透明、同步(所有服务器都成功后返回)、异步(本地服务器成功后返回…

键盘特殊_特殊键盘

键盘特殊Problem statement: 问题陈述: Imagine you have a special keyboard with four types of keys: 想象一下,您有一个特殊的键盘,其中包含四种类型的键: Key 1: Prints I on screen 按键1:在屏幕上打印“ I”…

【C++入门】简单的日期类操作

//--------------------------------------------------------------------------/***名称:日期的简单操作******类函数:构造函数,拷贝构造函数,析构函数,操作符重载函数****日期类操作函数: 1:…

Scala山脉

Scala Range Scala山脉 A Range is a bounded series with a uniform interval with an upper and lower limit. The range literal is a numerical sequence of number ranging with a certain limit. 范围是一个有上限且下限均匀的有界序列。 范围文字是具有一定限制的范围…

黑客经验谈:跳板攻击入侵技术实例解析

网络入侵,安全第一,一个高明的入侵者,不会冒然实行动. 他们在入侵时前会做足功课,入侵时会通过各种技术手段保护自己,以防被对方发现,引火烧身. 其中,跳板技术是攻击者通常采用的技术. 下面笔者结合实例&am…

dom属性和html属性_HTML属性

dom属性和html属性Attributes are used to provide additional information of a tag such as it’s alignments, color, size of the text and other. The attributes are given with the tag that is between the angular brackets after the tag name. The attributes have …

科普:UTF-8 GBK UTF8 GB2312 之间的区别和关系

UTF-8:Unicode TransformationFormat-8bit,允许含BOM,但通常不含BOM。是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为(三个字节)来编码…

vue3实现本地开发使用的px转换成vw,px转换成rem方法整理

前言: 项目中如果想本地开发使用px,但是界面上线以后界面是自适应的效果,可以有多种方式来实现效果。 一、px转成vw 1、安装,安装成功后,node_modules 会新增这两个插件包 npm i postcss-px-to-viewport-8-plugin 2、新增 post…

airplay2协议是什么_什么是AirPlay?

airplay2协议是什么AirPlay (AirPlay) AirPlay is released by Apple in the year 2004. It allows the easy exchange of audios without the use of any wired technique between the two devices. It was previously termed as AirTunes and later got its name changed to …

微信支付开发(5) 订单查询

本文介绍微信支付中订单查询功能的实现。 作者:方倍工作室 地址:http://www.cnblogs.com/txw1958/p/wxpay-order-query.html 一、订单查询 因为某一方技术的原因,可能导致商户在预期时间内都收不到最终支付通知,此时商户可以通过该…

ruby 执行函数_Ruby at()函数

ruby 执行函数Ruby中的at()函数 (at() function in Ruby) If you are working with arrays in Ruby, sometimes you may need to find the element at a particular index. For meeting the purpose, we have got at() function in Ruby which is already defined in Rubys lib…