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(键-值对)对构成的。
我们来看下面的例子:
Ruby:
hash = {
"start" => "hello world!"
}
print hash #=> {"start"=>"hello world"}
print hash["start"] #=> "hello world"
# 可以看到,我们使用{}(大括号)来定义一个哈希,接着在大括号内部
# 填充键-值对,键和值的中间使用=>(一个等于号+一个大于号)进行连接。
# 同时,对于访问数据来说,格式和数组访问数据是一样的,都是通过xxx[]这
# 样的一个格式来访问,只不过数组使用的是序号,而哈希使用的是键。
# 我们通过hash["start"]来访问hash这个哈希中“start”(开始)这个键的值。
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!”,同样也是一个字符串。
但是,键-值对的数据类型是可以是任意的,并不要求键和值的数据类型要一样。
我们来看下面的这个例子:
Ruby:
hash = {
"one" => 1,
2 => "two",
:three => ["three",3]
}
print hash #=> {"one"=>1,2=>"two",:three=>["three",3]}
print hash["one"] #=> 1
print hash[2] #=> "two"
print hash[:three] #=> ["three",3]
print hash[:three][1] #=> 3
# 可以看到,当我们想要录入多个键-值对时,我们使用“,”(逗号)进行分隔。
Now that we know this, we could technically also make a fake “array” with hashes as long as we use numbers for the keys.
如果我们使用数字作为哈希的键的话,我们就可以用哈希来模拟一个数组了,
请看下面的例子:
Ruby:
hash = {
0 => "zero",
1 => "one",
2 => "two"
}
print hash[0] #=> "zero"
print hash[1] #=> "one"
print hash[2] #=> "two"
array = [
"one",
"two",
"three"
]
print array[0] #=> "zero"
print array[1] #=> "one"
print array[2] #=> "two"
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:
跟数组一样,在获取到键的值之后,我们可以给这个键重新赋值,请看下面的例子:
Ruby:
hash = {
"three" => 3,
}
print hash["three"] #=> 3
hash["three"] = 9278343
print hash["three"] #=> 9278343
...Or we can add to it (if a key doesn’t exist in a hash, it returns nil):
而如果一个键不存在的话,就会返回nil。
我们也可以直接用xxx[yyy] = zzz这样的格式来给哈希录入更多的键-值对。
当我们这样做时,如果yyy本身在哈希里有这个键,那么就会对键重新赋值;如果哈希里没有这个键,那么就会录入这个键,并且给这个键赋值zzz。
我们来看下面的例子:
Ruby:
hash = {}
# 这是一个空哈希。
print hash["a key"] #=> nil
hash["a key"] = "a value"
# 录入了一个新的键-值对。
print hash["a key"] #=> "a value"
最后编辑: