Ruby-on-rails 数组和散列有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6097637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between arrays and hashes?
提问by emurad
What's the difference between arrays and hashes in Ruby?
Ruby 中的数组和散列之间有什么区别?
回答by intellidiot
From Ruby-Doc:
来自 Ruby-Doc:
Arrays are ordered, integer-indexed collections of any object. Array indexing starts at 0, as in C or Java. A negative index is assumed to be relative to the end of the array—that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on. Look herefor more.
数组是任何对象的有序、整数索引集合。数组索引从 0 开始,就像在 C 或 Java 中一样。负索引被假定为相对于数组的末尾——也就是说,索引 -1 表示数组的最后一个元素,-2 是数组中最后一个元素的下一个元素,依此类推。看这里了解更多。
A Hash is a collection of key-value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.
哈希是键值对的集合。它类似于数组,不同之处在于索引是通过任何对象类型的任意键完成的,而不是整数索引。哈希按照插入相应键的顺序枚举它们的值。
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil. Look herefor more.
哈希有一个默认值,当访问哈希中不存在的键时会返回该默认值。默认情况下,该值为零。看这里了解更多。
回答by Unknown Developer
Arrays: Arrays are used to store collections of data. Each object in an array has an unique key assigned to it. We can access any object in the array using this unique key. The positions in an array starts from " 0 ". The first element is located at " 0 ", the second at 1st position etc.
数组:数组用于存储数据集合。数组中的每个对象都有一个分配给它的唯一键。我们可以使用这个唯一键访问数组中的任何对象。数组中的位置从“0”开始。第一个元素位于“ 0 ”,第二个元素位于第一个位置等。
Example: Try the following in - irb.
示例:在 - irb 中尝试以下操作。
bikes = Array.new
bikes = %w[Bajaj-Pulsar, Honda-Unicorn, TVS-Apache, Yamaha, Suzuki]
You have added 4 elements in the array.
您已在数组中添加了 4 个元素。
puts bikes[3]
Yamaha,
Add a new element to position 5.
将新元素添加到位置 5。
bikes[5] = "Hardly Davidson"
Hashes: Like arrays, Hashes are also used to store data. Hashes points an object to another object. Consider of assigning a certain "meaning" to a string. Each time you refer that string, it refers its "meaning".
哈希:与数组一样,哈希也用于存储数据。哈希将一个对象指向另一个对象。考虑为字符串分配某种“含义”。每次您引用该字符串时,它都会引用其“含义”。
Example:
例子:
bikes = Hash.new
bikes = {
'Bajaj' => 'Pulsar 220, Pulsar 200, Pulsar 180 and Pulsar 150',
'Honda' => 'Unicorn, Shine and Splendor',
'TVS' => 'Apache, Star City, and Victor'
}
Try this now:
现在试试这个:
bikes['Bajaj']
You get => "Pulsar 220, Pulsar 200, Pulsar 180 and Pulsar 150"
你得到 =>“Pulsar 220、Pulsar 200、Pulsar 180 和 Pulsar 150”
回答by Quentin
An array is an ordered list of things: a, b, c, d
数组是事物的有序列表:a、b、c、d
A hash is a collection of key/value pairs: john has a peugeot, bob has a renault, adam has a ford.
哈希是键/值对的集合:约翰有一辆标致,鲍勃有一辆雷诺,亚当有一辆福特。
回答by James
The two terms get "hashed" together these days. I thinkthis is how it goes:
这些天这两个术语被“散列”在一起。我认为事情是这样的:
A "hash" will have key -> value pairs:
“散列”将具有键 -> 值对:
(top -> tshirt, bottom -> shorts, feet -> shoes)
(top -> tshirt, bottom -> shorts, feet -> shoes)
And an "array" will typically have an index:
一个“数组”通常会有一个索引:
([0]tshirt, [1]shorts, [2]shoes)
([0]tshirt, [1]shorts, [2]shoes)
But, right or wrong, you'll see things with key -> value pairs called "arrays", too.
但是,无论对错,您也会看到带有键 -> 值对的东西,也称为“数组”。
I think the difference depends mainly on when and how you want to use them. You won't get into much trouble calling an array a hash, or vice versa, but you should know the difference.
我认为区别主要取决于您想何时以及如何使用它们。将数组称为散列不会有太多麻烦,反之亦然,但您应该知道其中的区别。

