Hash.store(key,value)Ruby中的示例方法

Hash.store(key,value)方法 (Hash.store(key, value) Method)

In this article, we will study about Hash.store(key, value) Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this method with the help of its syntax and program code in the rest of the content.

在本文中,我们将研究Hash.store(key,value)Method 。 可以借助其名称来预测此方法的工作,但是它并不像看起来那样简单。 好了,我们将在其余内容中借助其语法和程序代码来理解此方法。

Method description:

方法说明:

This method is a public instance method that is defined in the ruby library especially for Hash class. This method works in a way that it stores or assigns the given value into the key with which the method has been invoked. This method takes two parameters, one is

此方法是在ruby库中定义的公共实例方法,特别是针对Hash类。 此方法的工作方式是,将给定值存储或分配给调用该方法的键。 此方法有两个参数,一个是

the key and another one is the value of that particular key.
This method does bring change in the actual hash because this method belongs to the category of destructive methods.

键,另一个是该特定键的值。
由于此方法属于破坏性方法的类别,因此该方法的确会带来实际哈希值的变化。

Syntax:

句法:

    Hash_object.store(key,value)

Argument(s) required:

所需参数:

This method takes two parameters, one is the key and another one is the value of that particular key.

此方法有两个参数,一个是键,另一个是该特定键的值。

Example 1:

范例1:

=begin
  Ruby program to demonstrate store method
=end	

hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash store implementation"

puts "Enter the key:"
ky = gets.chomp

puts "Enter the value:"
val = gets.chomp

hsh = hash1.store(ky,val)

puts  "Key updated is #{hsh}"

puts "Self hash object : #{hash1}"

Output

输出量

Hash store implementation
Enter the key:
 color
Enter the value:
 blue
Key updated is blue
Self hash object : {"color"=>"blue", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

说明:

In the above code, you can observe that we are storing values in the hash object with the help of the Hash.store() method. You can see how we can update the value of a particular key in the hash object with the help of this method? This method is creating changes in the actual hash object because this method is one of the examples of destructive methods.

在上面的代码中,您可以观察到我们借助于Hash.store()方法将值存储在哈希对象中。 您可以看到如何借助此方法更新哈希对象中特定键的值? 因为此方法是破坏性方法的示例之一,所以该方法正在实际的哈希对象中创建更改。

Example 2:

范例2:

=begin
  Ruby program to demonstrate store method
=end	

hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash store implementation"

hsh = hash1.store("City","Jaipur")

puts  "Value updated is #{hsh}"

puts "Self hash object : #{hash1}"

Output

输出量

Hash store implementation
Value updated is Jaipur
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato", "City"=>"Jaipur"}

Explanation:

说明:

In the above code, you can observe that we are storing values in the hash object with the help of the Hash.store() method. You can see that even if the key does not exist, this method will add a new key along with its value at the last index of the hash instance. This method is creating changes in the actual hash object because this method is one of the examples of destructive methods.

在上面的代码中,您可以观察到我们借助于Hash.store()方法将值存储在哈希对象中。 您可以看到,即使键不存在,此方法也会在哈希实例的最后一个索引处添加一个新键及其值。 因为此方法是破坏性方法的示例之一,所以该方法正在实际的哈希对象中创建更改。

翻译自: https://www.includehelp.com/ruby/hash-store-key-value-method-with-example.aspx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值