什么是红宝石阵列的方法和ASSOC的rassoc的目的是什么? [英] What is the purpose of Ruby array's methods assoc and rassoc?

查看:138
本文介绍了什么是红宝石阵列的方法和ASSOC的rassoc的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过的方法的文档 assoc命令 rassoc 红宝石。我知道他们做什么。他们都对数组的数组工作。 assoc命令返回的第一个元素等于提供的参数, rassoc 做同样的阵列,但对于第二个元素。

I've read the documentation of the methods assoc and rassoc of Ruby array. I understand what they do. They both work on arrays of arrays. assoc returns the array whose first element equals the provided parameter, and rassoc does the same, but for the second element.

我的问题是什么是这些方法?这个功能是非常具体的。我猜还有就是创建这些方法的一些原因。是什么呢?

My question is what are these methods for? This functionality is very specific. I'm guessing there is some reason why these methods were created. what is it?

推荐答案

有几个原因,但最主要的是,这样一种阵列可以像一个Hash行动。我认为,因为哈希不排序,Ruby的设计师需要一种方法来重新present哈希在一个有序的方式。我觉得有很多的一个散列调用时返回数组可枚举方法。

There are a couple of reasons, but the main one is so that an Array can act like a Hash. I think that because Hashes are unsorted, the Ruby designers needed a way to represent a hash in an ordered fashion. I think that there are a lot of Enumerable methods that return arrays when invoked on a hash.

这篇关于什么是红宝石阵列的方法和ASSOC的rassoc的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆