Ruby 中的关联数组 [英] Associative Arrays in Ruby

查看:27
本文介绍了Ruby 中的关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby 有关联数组吗?

Does Ruby have associative arrays?

例如:

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

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

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

推荐答案

与将数组和散列混为一谈的 PHP 不同,在 Ruby(以及几乎所有其他语言)中,它们是一个独立的东西.

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

你的情况是:

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

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

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

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

这篇关于Ruby 中的关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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