Ruby 中的关联数组

发布于 2024-10-03 19:28:27 字数 152 浏览 0 评论 0原文

Ruby 有关联数组吗?

例如:

   a = Array.new
   a["Peter"] = 32
   a["Quagmire"] = 'asdas'

在 Ruby 中创建此类数据结构的最简单方法是什么?

Does Ruby have associative arrays?

For eg:

   a = Array.new
   a["Peter"] = 32
   a["Quagmire"] = 'asdas'

What is the easiest method to create such an data structure in Ruby?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蒗幽 2024-10-10 19:28:27

与将数组和哈希合并在一起的 PHP 不同,在 Ruby(以及几乎所有其他语言)中,它们是独立的东西。

http://ruby-doc.org/core/classes/Hash.html

在你的情况下,它是:

a = {'Peter' => 32, 'Quagmire' => 'asdas'}

有几本免费的关于 ruby​​ 和在线模拟器等的介绍性书籍。

http:// www.ruby-doc.org/

Unlike PHP which conflates arrays and hashes, in Ruby (and practically every other language) they're a separate thing.

http://ruby-doc.org/core/classes/Hash.html

In your case it'd be:

a = {'Peter' => 32, 'Quagmire' => 'asdas'}

There are several freely available introductory books on ruby and online simulators etc.

http://www.ruby-doc.org/

零時差 2024-10-10 19:28:27

使用哈希,这里有一些关于如何开始的示例(所有这些都做同样的事情,只是语法不同):

a = Hash.new
a["Peter"] = 32
a["Quagmire"] = 'asdas'

或者你可以这样做:

a = {}
a["Peter"] = 32
a["Quagmire"] = 'asdas'

或者甚至是单行:

a = {"Peter" => 32, "Quagmire" => 'gigity'}

Use hashes, here's some examples on how to get started (all of these do the same thing, just different syntax):

a = Hash.new
a["Peter"] = 32
a["Quagmire"] = 'asdas'

Or you could do:

a = {}
a["Peter"] = 32
a["Quagmire"] = 'asdas'

Or even a one liner:

a = {"Peter" => 32, "Quagmire" => 'gigity'}
深爱不及久伴 2024-10-10 19:28:27

为了完整起见,值得注意的是 Ruby 确实提供了Array#assocArray#rassoc 方法为数组数组添加“类似散列的查找”:

arr = [
  ['London', 'England'],
  ['Moscow', 'Russia'],
  ['Seattle', 'USA']
]

arr.assoc('Seattle') #=> '['Seattle', 'USA']
arr.rassoc('Russia') #=> ['Moscow', 'Russia']

请记住,与 Ruby 散列不同,其中查找时间是常量 O(1)assocrassoc 都具有线性时间 O(n)。您可以通过查看 Ruby 源代码来了解原因每个方法的 Github。

因此,尽管理论上您可以在 Ruby 中使用数组组成的数组来实现“类似散列”,但您可能只想使用 assoc/rassoc 方法,如果你给定一个数组数组 - 也许通过一些你无法控制的外部 API - 否则在几乎所有其他情况下,使用哈希将是更好的路线。

For completeness, it's interesting to note that Ruby does provide Array#assoc and Array#rassoc methods that add "hash like lookup" for an array of arrays:

arr = [
  ['London', 'England'],
  ['Moscow', 'Russia'],
  ['Seattle', 'USA']
]

arr.assoc('Seattle') #=> '['Seattle', 'USA']
arr.rassoc('Russia') #=> ['Moscow', 'Russia']

Keep in mind that unlike a Ruby hash where lookup time is a constant O(1), both assoc and rassoc have a linear time O(n). You can see why this is by having a look at the Ruby source code on Github for each method.

So, although in theory you can use an array of arrays to be "hash like" in Ruby, it's likely you'll only ever want to use the assoc/rassoc methods if you are given an array of arrays - maybe via some external API you have no control over - and otherwise in almost all other circumstances, using a Hash will be the better route.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文