ruby hash添加数据

Before going through the ways to add elements to the hash instances, let us understand what could be called as a hash element. So, Hash is the collection of keys and their values. For example,

在介绍向哈希实例添加元素的方法之前,让我们了解什么可以称为哈希元素。 因此, 哈希是键及其值的集合。 例如,

    Hash1 = {"Color" => "Red"} # is a hash object as it has a key value pair.

Now, let us understand the different ways through which we can add elements in the hash object.

现在,让我们了解在哈希对象中添加元素的不同方法。

Method 1: Use Hash.store() method

方法1:使用Hash.store()方法

This method is a public instance method that is defined in the ruby library especially for the 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 the key and another one is the value of that particular key.

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

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)

Parameter(s) required:

所需参数:

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

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

Program:

程序:

=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:
country
Enter the value:
India
Key updated is India
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends",
"fruit"=>"Kiwi", "vege"=>"potato", "country"=>"India"}

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()方法将值存储在哈希对象中。 您将看到如何使用此方法来更新哈希对象中特定键的值。 因为此方法是破坏性方法的示例之一,所以该方法正在实际的哈希对象中创建更改。

Method 2: With the help of Hash.merge() method

方法2:借助于Hash.merge()方法

You can add elements in a particular hash with the help of another hash as well. You only have to store elements that you want to add in a different hash and apply merge operation.

您也可以在另一个哈希中添加元素。 您只需要将要添加的元素存储在其他哈希中,然后应用合并操作即可。

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. This method works in a way that it returns a new hash object which contains the keys and values of self hash as well as another hash. If both the hashes are containing the same keys and values then the new hash will not contain duplicate keys and values or you can say that each key and value will be stored only for once. This method is one of the examples of non-destructive methods where the changes created by the methods are temporary or non-permanent.

此方法是Public实例方法,属于Hash类,它位于Ruby语言库中。 此方法的工作方式是返回一个新的哈希对象,该对象包含自身哈希的键和值以及另一个哈希。 如果两个哈希都包含相同的键和值,则新的哈希将不包含重复的键和值,或者可以说每个键和值仅存储一次。 此方法是非破坏性方法的示例之一,其中方法所产生的更改是临时的或非永久的。

Syntax:

句法:

    Hash_object.merge(other_hash)

Parameter(s) required:

所需参数:

This method only takes one parameter and that argument is nothing but another hash instance you want to merge.

此方法仅使用一个参数,该参数不过是您要合并的另一个哈希实例。

Program:

程序:

=begin
Ruby program to demonstrate
Hash.merge(other_hash) method
=end
hsh={"colors"=>"red","letters"=>"a","Fruit"=>"Grapes","anything"=>"red","sweet"=>"ladoo"}
hsh1={"home"=>"shivaliknagar","city"=>"Haridwar","state"=>"Uttrakhand"}
puts "Hash.merge implementation:"
hash3 = hsh.merge(hsh1)
puts "The keys present in the new hash are: #{hash3}"
puts "Original hash : #{hsh}"

Output

输出量

Hash.merge implementation:
The keys present in the new hash are: {"colors"=>"red", "letters"=>"a",
"Fruit"=>"Grapes", "anything"=>"red", "sweet"=>"ladoo", "home"=>"shivaliknagar",
"city"=>"Haridwar", "state"=>"Uttrakhand"}
Original hash : {"colors"=>"red", "letters"=>"a", "Fruit"=>"Grapes",
"anything"=>"red", "sweet"=>"ladoo"}

Explanation:

说明:

In the above code, you can observe that you can merge with another hash with the help of the Hash.merge() method. You can see that the new hash is containing the keys and values of both the hashes. This method is not creating any changes in the original hash because this method is an example of non-destructive methods where the changes created by the method are non-permanent.

在上面的代码中,您可以观察到可以借助Hash.merge()方法与另一个哈希合并。 您可以看到新的哈希包含两个哈希的键和值。 此方法不会在原始哈希中创建任何更改,因为此方法是非破坏性方法的一个示例,该方法所创建的更改是非永久性的。

翻译自: https://www.includehelp.com/ruby/how-to-add-elements-to-a-hash-in-ruby.aspx

ruby hash添加数据

ruby hash添加数据_如何在Ruby中向Hash添加元素?相关推荐

  1. php oracle 操作 sql语句中能不能添加数组_如何在PHP中使用Oracle数据库_php

    在php3.0以上版本中,php内置了几乎目前所有的数据库处理函数,包括oracle;在本文中我们通过一个实例来介绍了如何使用这些函数来操作Oracle数据库. PHP提供了2大类API(应用程序接口 ...

  2. ios表格添加底纹_如何在Word中的表格单元格中添加底纹

    ios表格添加底纹 If you have a table with many rows and columns, it can be difficult to read. Adding shadin ...

  3. python一行输出多个数据_如何在Python中让两个print()函数的输出打印在一行内?

    1.两个连续的print()函数为什么在输出时内容会分行显示? 解:print()中有两个默认参数sep和end,其中sep是代替分隔符,end是代替末尾的换行符,默认使用','代替空格,且默认末尾加 ...

  4. wordpress添加媒体_如何在WordPress中添加jQuery FAQ手风琴

    wordpress添加媒体 Recently one of our users asked us if there was a way for them to add a FAQ accordion ...

  5. wordpress添加媒体_如何在WordPress中添加新帖子并利用所有功能

    wordpress添加媒体 Are you trying to create a new post in WordPress? Do you want to learn about all the W ...

  6. wordpress添加媒体_如何在WordPress中添加精选内容滑块

    wordpress添加媒体 Do you want to add a featured content slider like Yahoo or ESPN? Many popular sites us ...

  7. wordpress添加媒体_如何在WordPress中添加社交媒体共享计数小部件

    wordpress添加媒体 Earlier on this blog, we created a Social Media Cheat Sheet for WordPress, but that on ...

  8. wordpress添加媒体_如何在WordPress中添加电子书下载

    wordpress添加媒体 Do you want to add ebook downloads to your WordPress site? WordPress makes it easy for ...

  9. ruby字符串截取字符串_如何在Ruby中附加字符串?

    ruby字符串截取字符串 There are multiple ways to do the required but we will study about three of them. 有多种方法 ...

最新文章

  1. VMWare不能安装64位操作系统原因探析
  2. 009_logback本地Appender
  3. mvc html.hidden,MVC Html.HiddenFor在一个循环传递模型回控制器
  4. VC++ _T()宏学习
  5. 10 个超炫酷后台控制面板(附 GitHub下载链接)
  6. 1.1-1.5-vim编辑器
  7. 自动加密可序列化的类
  8. 创建线程都有哪些方式?— Callable篇
  9. 【模块化开发】之 Webpack、Rollup、Parcel
  10. 查看函数说明_Axure函数使用说明
  11. bat脚本保存dir结果_Tomcat的启停脚本源码解析
  12. Python Imaging Library: ImagePath Module(图像路径模块)
  13. pygame检测精灵与精灵的碰撞_Pygame(三)--走出黑暗的洞穴(2)
  14. VAssistX的卸载方法
  15. 2020年度20多款主流数据库重大更新及技术要点回顾
  16. 如何生成玫瑰?|数字艺术
  17. 精彩回顾 | 苏州农商银行新一代云原生信息科技架构体系实践
  18. 一度智信在拼多多上开店靠谱吗?
  19. Android Settings 中wifi详情页 隐私默认选中设备mac
  20. 如何设计一个混沌工程实验?

热门文章

  1. mysql collect_set_hive列转行 (collect_set())
  2. MySQL管理工具MySQL Utilities — 如何连接MySQL服务器
  3. go设计模式之装饰器模式
  4. C#异或运算符的使用【C#】
  5. 【洛谷 P2513】 [HAOI2009]逆序对数列(DP)
  6. 三张图搞懂JavaScript的原型对象与原型链
  7. 自定义控件---重写Listbox实现item图标变换和item点击事件
  8. wdcp支持两种安装方式
  9. hbase 学习(十三)集群间备份原理
  10. android四大组件之Service 注册广播接收者