as_hash ruby_Hash.merge(other_hash)方法与Ruby中的示例

as_hash ruby

Hash.merge(other_hash)方法 (Hash.merge(other_hash) Method)

In this article, we will study about Hash.merge(other_hash) Method. The working of the method can’t be assumed because it’s quite a different name. Let us read its definition and understand its implementation with the help of syntax and program codes.

在本文中,我们将研究Hash.merge(other_hash)方法 。 无法假定该方法的工作原理,因为它的名称完全不同。 让我们阅读其定义并在语法和程序代码的帮助下了解其实现。

Method description:

方法说明:

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)

Argument(s) required:

所需参数:

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

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

Example 1:

范例1:

=begin
Ruby program to demonstrate Hash.merge(other_hash) method
=end	
hsh = {"colors"  => "red","letters" => "a", "Fruit" => "Grapes", "anything"=>"red","sweet"=>"ladoo"}
hsh1 = {"home" => "shivalik nagar", "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"=>"shivalik nagar", "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 nonpermanent.

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

Example 2:

范例2:

=begin
Ruby program to demonstrate 
Hash.merge(other_hash) method
=end	
hsh = {"home" => "shivalik nagar", "city"=>"Haridwar","state"=>"Uttrakhand"}
hsh1 = {"home" => "shivalik nagar", "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: {"home"=>"shivalik nagar", "city"=>"Haridwar", "state"=>"Uttrakhand"}
Original hash : {"home"=>"shivalik nagar", "city"=>"Haridwar", "state"=>"Uttrakhand"}

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 no- duplicate keys and values even though there is a repetition of keys in both the hashes but the new hash is storing them only for once. 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 nonpermanent.

在上面的代码中,您可以观察到可以借助Hash.merge()方法与另一个哈希合并。 您可以看到,即使两个哈希中都有键的重复,新哈希也包含不重复的键和值,但是新哈希仅将它们存储一次。 此方法不会在原始哈希中创建任何更改,因为此方法是非破坏性方法的示例,其中该方法创建的更改是永久的。

翻译自: https://www.includehelp.com/ruby/hash-merge-other_hash-method-with-example.aspx

as_hash ruby

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541727.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux 安装nfs 客户端,在CentOS 7上安装NFS服务器和客户端

NFS服务器和客户端安装在CentOS 7上版本1.0作者:Srijan Kishore 在Twitter上关注howtoing最后编辑 16 / Dec / 2014本指南介绍如何在CentOS 7.0中配置NFS服务器网络文件系统(NFS)是一种流行的分布式文件系统协议,可让用户在其服务器上安装远程目录。 该系…

安装ORACLE 时报错 /jre/1.4.2/lib/i386/libawt.so:

最近在linux下安装oracle 10g时,碰到如下问题: /tmp/OraInstall2011-09-11_02-16-11PM/jre/1.4.2/lib/i386/libawt.so: libXp.so.6: cannot open shared object file: No such file or directory occurred.. 网上找了下,真让人费解呀&am…

Java线程start()vs run()方法及示例

Java | 线程start()vs run()方法 (Java | Thread start() vs run() Methods) When we call the start() method, it leads to the creation of a new thread. Then, it automatically calls the run() method. If we directly call the run() method, then no new thread will …

linux安装卸载mysql,Linux6 系列 安装、卸载mysql

Linux6 系列 安装、卸载mysqlLinux6 系列 安装、卸载mysqlLinux环境下载mysql:https://blog.csdn.net/weixin_40816738/article/details/90111456一、安装环境依赖:yum install -y cmake make gcc gcc-c libaio ncurses ncurses-devel二、安装流程1、软件…

Python | 如何使用pip升级所有Python软件包?

While using Python as a programming language, its a very common scenario to use a virtual environment and PIP, a package manager for python. 当使用Python作为编程语言时,使用虚拟环境和PIP (Python的程序包管理器)是一种非常常见的情况。 Its a common …

linux下enum类型占几个字节,enum大小问题

问题描述板卡有两个CPU,ARMMIPS,同时运行三个系统REE(linux) TEE(SierraTEE) SEE(TDS)。TEE跟SEE通过RPC进行通信,有enum成员的结构体信息传递会出错,如下结构体:struct sTag {enum A;enum B;int C;enum D;};问题分析…

ASP.NET导出word实例

ASP.NET导出word实例 最近遇到一个题目就是如何在asp.net中将数据导出到word中,由于数据是动态的,所以需要在后台拼出想要的的格式,翻遍了网页找出了一个比较满意的代码,感谢那位高手。代码如下: public void Download…

Java LocalDate类| toString()方法与示例

LocalDate类toString()方法 (LocalDate Class toString() method) toString() method is available in java.time package. toString()方法在java.time包中可用。 toString() method is used to represent this LocalDate as a String by using the standards ISO-8601 format.…

linux14.04 Apache,Ubuntu 14.04编译安装Apache

Ubuntu下编译安装apache需要预先编译安装多个依赖件,包括:apr, apr-util,pcre,zlib-devel,等,相当麻烦,记录于此备查.由于Ubuntu系统默认安装时没有安装C,所以也需要先安装c编译需要相关的组件。[注]apt-ca…

Android Jenkins自动化构建之路

install Jenkins 添加Jenkins的源(repository): sudo wget -O /etc/yum.repos.d/jenkins.repo http://jenkins-ci.org/redhat/jenkins.repo sudo rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key复制代码yum install Jenkins复制代码…

java 根据类名示例化类_Java即时类| plusMillis()方法与示例

java 根据类名示例化类即时类plusMillis()方法 (Instant Class plusMillis() method) plusMillis() method is available in java.time package. plusMillis()方法在java.time包中可用。 plusMillis() method is used to add the given duration in milliseconds to this Insta…

linux dd入门,Linux基础知识:Linux中DD命令详解

1.dd命令简介功能:把指定的输入文件拷贝到指定的输出文件中,并且在拷贝过程中可以进行格式转换。可以用该命令实现DOS下的diskcopy命令的作用。先用dd命令把软盘上的数据写成硬盘的一个寄存文件,再把这个寄存文件写入第二张软盘上&#xff0c…

CSS 字体(font)实例

1、设置文本字体 font-family:"Times New Roman",Georgia,Serif font-family:Arial,Verdana,Sans-serif 2、设置字体尺寸 font-size: 100% 3、设置字体风格 font-style:normal font-style:italic font-style:oblique 4、设置字体的异体 font-variant:normal text-var…

Java Duration类| 带示例的compareTo()方法

持续时间类compareTo()方法 (Duration Class compareTo() method) compareTo() method is available in java.time package. compareTo()方法在java.time包中可用。 compareTo() method is used to compare this Duration object to the given object. compareTo()方法用于将此…

linux定时任务执行url,科技常识:linux定时任务访问url实例

今天小编跟大家讲解下有关linux定时任务访问url实例 ,相信小伙伴们对这个话题应该也很关注吧,小编也收集到了有关linux定时任务访问url实例 的相关资料,希望小伙伴会喜欢也能够帮助大家。这次linux定时任务设置成功,也算是自己学习…

lcase和ucase_在SQL中使用UCASE(),LCASE()和MID()函数

lcase和ucaseUpper Case, Lower Case and MID functions are scalar functions which return a single value, based in the input value. 大写,小写和MID函数是标量函数,它们基于输入值返回单个值。 As you all know sometimes different databases ha…

Maven的Settings.xml配置文件解释

该配置用于单用户配置和全局配置, 单用户配置默认存放于 ${user.home}/.m2/目录中. 全局配置默认存放于Maven安装目录下面的conf目录中. 这两个默认的位置都可以修改. <?xml version"1.0" encoding"UTF-8"?> <settings xmlns"http://m…

linux ntp手动授时,关于我校NTP授时服务的使用说明

校园网用户&#xff1a;我中心于近期采购了GPS北斗授时服务设备&#xff0c;该设备可实现纯GPS模式、纯北斗模式和混合模式与卫星对时&#xff0c;同时实现对校内设备授时的功能。支持所有NTP协议的服务器、PC、嵌入式设备等&#xff0c;包括但不限于&#xff1a;Microsoft Win…

一串字符串转换为ascii_将ASCII字符串(char [])转换为C中的BYTE数组

一串字符串转换为asciiGiven an ASCII string (char[]) and we have to convert it into BYTE array (BYTE[]) in C. 给定一个ASCII字符串(char [])&#xff0c;我们必须将其转换为C语言中的BYTE数组(BYTE [])。 Logic: 逻辑&#xff1a; To convert an ASCII string to BYTE…

debugging Auto Layout:Logical Errors

Logical Errors逻辑错误 Logical errors are simply bugs. Somewhere, you have an assumption that is faulty. Perhaps it’s an assumption about how Auto Layout calculates the views’ frames. Perhaps it’s an assumption about the set of constraints that you’ve …