ruby 生成哈希值_Ruby中的哈希

ruby 生成哈希值

Arrays are not the only way to manage collections of variables in Ruby. Another type of collection of variables is the hash, also called an associative array. A hash is like an array in that it's a variable that stores other variables. However, a hash is unlike an array in that the stored variables are not stored in any particular order, and they are retrieved with a key instead of by their position in the collection.

数组并不是管理Ruby中变量集合的唯一方法。 变量集合的另一种类型是hash ,也称为关联数组。 哈希就像一个数组,因为它是一个存储其他变量的变量。 但是,哈希与数组不同,因为存储的变量没有以任何特定顺序存储,而是使用键而不是通过它们在集合中的位置来检索它们。

使用键/值对创建哈希 ( Create a Hash With Key/Value Pairs )

A hash is useful to store what are called key/value pairs. A key/value pair has an identifier to signify which variable of the hash you want to access and a variable to store in that position in the hash. For example, a teacher might store a student's grades in a hash. Bob's grade would be accessed in a hash by the key "Bob" and the variable stored at that location would be Bob's grade.

散列对于存储所谓的键/值对很有用。 键/值对具有一个标识符,表示要访问的哈希变量, 以及一个存储在哈希中该位置的变量。 例如,老师可能将学生的成绩存储在哈希中。 Bob的成绩将通过键“ Bob”以散列方式访问,并且存储在该位置的变量将是Bob的成绩。

A hash variable can be created the same way as an array variable. The simplest method is to create an empty hash object and fill it with key/value pairs. Note that the index operator is used, but the student's name is used instead of a number.​​

可以使用与数组变量相同的方式来创建哈希变量。 最简单的方法是创建一个空的哈希对象,并用键/值对填充它。 请注意,使用的是索引运算符,但使用的是学生的姓名而不是数字。

Remember that hashes are unordered, meaning there is no defined beginning or end as there is in an array. So, you cannot append to a hash. Values are simply inserted into the hash using the index operator.

请记住,哈希是无序的,这意味着没有定义的开始或结束,就像数组中一样。 因此,您不能追加到哈希。 只需使用索引运算符将值插入哈希即可。


#!/usr/bin/env ruby
grades = Hash.new
grades["Bob"] = 82
grades["Jim"] = 94
grades["Billy"] = 58
puts grad

散列文字 ( Hash Literals )

Just like arrays, hashes can be created with hash literals. Hash literals use the curly braces instead of square brackets and the key value pairs are joined by =>. For example, a hash with a single key/value pair of Bob/84 would look like this: { "Bob" => 84 }. Additional key/value pairs can be added to the hash literal by separating them with commas. In the following example, a hash is created with the grades for a number of students.

就像数组一样,可以使用散列文字创建散列 散列文字使用大括号而不是方括号,并且键值对由=>连接 。 例如,具有Bob / 84的单个键/值对的哈希看起来像这样: {“ Bob” => 84} 。 可以通过使用逗号将其他键/值对添加到哈希文字中。 在以下示例中,将创建一个哈希值,其中包含许多学生的成绩。


#!/usr/bin/env ruby
grades = { "Bob" => 82,
"Jim" => 94,
"Billy" => 58
}
puts grad

在哈希中访问变量 ( Accessing Variables in the Hash )

There may be times when you must access each variable in the hash. You can still loop over the variables in the hash using the each loop, though it won't work the same way as using the each loop with array variables. Because a hash is unordered, the order in which each will loop over the key/value pairs may not be the same as the order in which you inserted them. In this example, a hash of grades will be looped over and printed.

有时您必须访问哈希中的每个变量。 您仍然可以使用each循环遍历散列中的变量,尽管它的工作方式不同于将each循环与数组变量一起使用。 由于哈希是无序的,因此每个哈希在键/值对上的循环顺序可能与您插入键/值对的顺序不同。 在此示例中,将遍历并打印成绩的哈希值。


#!/usr/bin/env ruby
grades = { "Bob" => 82,
"Jim" => 94,
"Billy" => 58
}
grades.each do|name,grade|
puts "#{name}: #{gra

翻译自: https://www.thoughtco.com/how-to-create-hashes-2908196

ruby 生成哈希值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值