9: Hashes 哈希
Introducing yet another data type: Hash. Hashes are very much like arrays in that it’s a list of objects with a specific location in the array/hash. The key difference (pun intended) is that objects inside arrays are always at a specific index which is a whole number between 0 and, well, infinity, whereas the “indexes” of objects inside a hash can be pretty much any object: a symbol, a string, a number, an array, another hash, or even a class.
Hashes are denoted by typing a bunch of key => value structures, separated by a comma, within two curly brackets. Here, key stands for an object of some kind, and value stands for an object of some kind. These don’t have to be the same.
现在我们来学习另一种数据类型:哈希。
哈希其实是Hash的音译,哈希简单的说的话其实就是更加“高级”的数组。
所以,数组有的一些特点,哈希全都有。
哈希和数组的不同点在于,数组通过元素的序号和值进行关联;
而哈希几乎可以使用“任何东西”和值进行关联,用来关联的这个“东西”,我们称为key,也就是键。
也就是说,哈希其实是由一对一对的key-value(键-值对)对构成的。
我们来看下面的例子:
Like most other things in Ruby, spaces and newlines are almost always disregarded. It doesn’t matter how many or how little whitespace you have.
在Ruby中,空格和是否新起一行在不产生歧义的情况下对代码是没有影响的,虽然我可能应该在更早的时候就说到这一点。
In this example we see a hash with one key-value pair. The key is “start”, and the value that belongs to that key (in arrays, index) is “hello world!”.
Those keys aren’t limited to just strings (the logical choice, because they’re more descriptive than, say, 482 as a key), but you can also use other data types:
在上面的例子中,hash这个哈希中有一个键-值对,其中键是“start”,是一个字符串,而值是“hello world!”,同样也是一个字符串。
但是,键-值对的数据类型是可以是任意的,并不要求键和值的数据类型要一样。
我们来看下面的这个例子:
Now that we know this, we could technically also make a fake “array” with hashes as long as we use numbers for the keys.
如果我们使用数字作为哈希的键的话,我们就可以用哈希来模拟一个数组了,
请看下面的例子:
From this point it’s basically like a regular ol’ array. We can use the keys to access the objects in the hash and change their values:
跟数组一样,在获取到键的值之后,我们可以给这个键重新赋值,请看下面的例子:
...Or we can add to it (if a key doesn’t exist in a hash, it returns nil):
而如果一个键不存在的话,就会返回nil。
我们也可以直接用xxx[yyy] = zzz这样的格式来给哈希录入更多的键-值对。
当我们这样做时,如果yyy本身在哈希里有这个键,那么就会对键重新赋值;如果哈希里没有这个键,那么就会录入这个键,并且给这个键赋值zzz。
我们来看下面的例子:
Introducing yet another data type: Hash. Hashes are very much like arrays in that it’s a list of objects with a specific location in the array/hash. The key difference (pun intended) is that objects inside arrays are always at a specific index which is a whole number between 0 and, well, infinity, whereas the “indexes” of objects inside a hash can be pretty much any object: a symbol, a string, a number, an array, another hash, or even a class.
Hashes are denoted by typing a bunch of key => value structures, separated by a comma, within two curly brackets. Here, key stands for an object of some kind, and value stands for an object of some kind. These don’t have to be the same.
现在我们来学习另一种数据类型:哈希。
哈希其实是Hash的音译,哈希简单的说的话其实就是更加“高级”的数组。
所以,数组有的一些特点,哈希全都有。
哈希和数组的不同点在于,数组通过元素的序号和值进行关联;
而哈希几乎可以使用“任何东西”和值进行关联,用来关联的这个“东西”,我们称为key,也就是键。
也就是说,哈希其实是由一对一对的key-value(键-值对)对构成的。
我们来看下面的例子:
Like most other things in Ruby, spaces and newlines are almost always disregarded. It doesn’t matter how many or how little whitespace you have.
在Ruby中,空格和是否新起一行在不产生歧义的情况下对代码是没有影响的,虽然我可能应该在更早的时候就说到这一点。
In this example we see a hash with one key-value pair. The key is “start”, and the value that belongs to that key (in arrays, index) is “hello world!”.
Those keys aren’t limited to just strings (the logical choice, because they’re more descriptive than, say, 482 as a key), but you can also use other data types:
在上面的例子中,hash这个哈希中有一个键-值对,其中键是“start”,是一个字符串,而值是“hello world!”,同样也是一个字符串。
但是,键-值对的数据类型是可以是任意的,并不要求键和值的数据类型要一样。
我们来看下面的这个例子:
Now that we know this, we could technically also make a fake “array” with hashes as long as we use numbers for the keys.
如果我们使用数字作为哈希的键的话,我们就可以用哈希来模拟一个数组了,
请看下面的例子:
From this point it’s basically like a regular ol’ array. We can use the keys to access the objects in the hash and change their values:
跟数组一样,在获取到键的值之后,我们可以给这个键重新赋值,请看下面的例子:
...Or we can add to it (if a key doesn’t exist in a hash, it returns nil):
而如果一个键不存在的话,就会返回nil。
我们也可以直接用xxx[yyy] = zzz这样的格式来给哈希录入更多的键-值对。
当我们这样做时,如果yyy本身在哈希里有这个键,那么就会对键重新赋值;如果哈希里没有这个键,那么就会录入这个键,并且给这个键赋值zzz。
我们来看下面的例子: