ruby 生成哈希值_Ruby中的哈希

本文深入讲解了Ruby中的哈希概念,包括如何创建哈希、使用哈希存储和检索值,以及通过示例演示哈希的基本操作。哈希在Ruby中是一种重要的数据结构,允许使用键值对进行高效的数据存储和检索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ruby 生成哈希值

Ruby Hash集合 (Ruby Hash Collection)

In Ruby, hashes are the collection of identical keys and their values. They are like dictionaries, which has an element along with its description. You can retrieve any available value with the help of a key associated with it. It is alike arrays, but here, you can use indexes of an object type, instead of only integer types. Hashes can also be termed as dictionaries, maps, and associative arrays.

在Ruby中, 散列是相同键及其值的集合 。 它们就像字典一样,具有其说明和元素。 您可以借助与之关联的键来检索任何可用值。 它类似于数组,但是在这里,您可以使用对象类型的索引,而不仅仅是整数类型。 哈希也可以称为字典,地图和关联数组。

You will get nil as return statement if you try to access a hash with a key and that key does not exist.

如果尝试使用键访问哈希并且该键不存在,则将nil作为返回语句。

Now, let us see how we create a hash in Ruby?

现在,让我们看看如何在Ruby中创建哈希?

You can create a hash inside {} (curly braces). => symbol is required to provide values to the key and to provide multiple key/values to the hash, a comma is used.

您可以在{} (大括号)内创建哈希。 =>符号是向键提供值并向哈希提供多个键/值所必需的,使用逗号。

Syntax:

句法:

    hash_name = {"key1"=> "value", "key2"=> "value", "key3"=> "value"}
    #or
    hash_name["key"] = "value"
    #or
    hash_name = {key1:  "value1", key2:  "value2", key3:  "value3"}

Now, let us understand the concept with the help of some examples given below,

现在,让我们借助下面给出的一些示例来理解这一概念,

Example 1:

范例1:

id = {   
	"AA101" => "Suresh",   
	"AA102" => "Sanjiv",   
	"AA103" => "Dam",   
	"AA104" => "Swati",
	"AA105" => "Kamlesh"
}      

id.each do |key, value|   
puts "#{key} = #{value}"   
end  

Output

输出量

AA101 = Suresh
AA102 = Sanjiv
AA103 = Dam
AA104 = Swati
AA105 = Kamlesh

Explanation:

说明:

In the above code, we have created a hash with our values by putting it inside curly braces. We are then printing the hash with the help of each method. The hash is having the record of students along with their id. This is the advantage of hashes that we don't have only integer indexes.

在上面的代码中,我们通过将其放在花括号中创建了带有值的哈希 。 然后,我们将在每种方法的帮助下打印哈希哈希表具有学生的记录及其ID。 这是哈希值的优势,因为我们没有整数索引。

Example 2:

范例2:

fruit = {"banana"=>"yellow"}
puts "Enter the number of key/value you want to enter"
i = gets.chomp.to_i
while (i!=0)
	puts "Enter key:"
	x = gets.chomp
	puts "Enter value:"
	v = gets.chomp
	fruit.store(x,v)
	i=i-1
end
fruit.each do |key, value|   
puts "#{key}'s color is #{value}" 
end  

Output

输出量

Enter the number of key/value you want to enter
4
Enter key:
Apple
Enter value:
Red
Enter key:
Grapes
Enter value:
Green
Enter key:
Mango
Enter value:
Yellow
Enter key:
Kiwiw
Enter value:
Green
banana's color is yellow
Apple's color is Red
Grapes's color is Green
Mango's color is Yellow
Kiwiw's color is Green

Explanation:

说明:

In the above program, we are trying to create a hash with the help of user input values. We have first declared a hash with our values. Then we have asked the user for the number of values she wants to store in the hash. We are making use of the hash.store method to store the user input keys and their corresponding values. At last, we are printing all the values with the help of .each method.

在上面的程序中,我们试图在用户输入值的帮助下创建一个哈希 。 我们首先用值声明了一个哈希值。 然后,我们向用户询问了她要存储在哈希中的值的数量。 我们正在使用hash.store方法存储用户输入键及其对应的值。 最后,我们将在.each方法的帮助下打印所有值。

翻译自: https://www.includehelp.com/ruby/hashes.aspx

ruby 生成哈希值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值