ruby hash方法_Ruby中带有示例的Hash.rehash方法

ruby hash方法

Hash.rehash方法 (Hash.rehash Method)

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

在本文中,我们将研究Hash.rehash方法 。 由于该方法的名称完全不同,因此无法进行假设。 让我们阅读其定义并在语法和程序代码的帮助下了解其实现。

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 constructs a new hash object which is based on the current hash object values for individual keys. If there is a change introduced in the hash object after they are declared then this method will do a re-indexing of the keys in the hash. You will have to face a run time error if you are trying to invoke this function during the process of traversing the hash instance.

此方法是Public实例方法,属于Hash类,它位于Ruby语言库中。 此方法以构造新哈希对象的方式工作,该对象基于单个键的当前哈希对象值。 如果声明了哈希对象后引入了更改,则此方法将对哈希中的键进行重新索引。 如果在遍历哈希实例的过程中尝试调用此函数,则将面临运行时错误。

Syntax:

句法:

    Hash_object.rehash

Argument(s) required:

所需参数:

This method does not require any arguments.

此方法不需要任何参数。

Example 1:

范例1:

=begin
Ruby program to demonstrate Hash.rehash method
=end
a = ["Satyam","Amisha"]
b = ["Nikhil","Saksham"]
hsh = {a=>"friends", b=>"friends"}
puts "Hash elements are: #{hsh}"
puts "Hash.rehash implementation"
b[0]="Hrithik"
puts "Hash after rehash: #{hsh.rehash}"

Output

输出量

Hash elements are: {["Satyam", "Amisha"]=>"friends", ["Nikhil", "Saksham"]=>"friends"}
Hash.rehash implementation
Hash after rehash: {["Satyam", "Amisha"]=>"friends", ["Hrithik", "Saksham"]=>"friends"}

Explanation:

说明:

In the above code, you can observe that you can rehash a hash object with the help of the Hash.rehash() method. We have inserted a new element inside the hash and replaced the current one. We got the result after rehashing the hash instance. This method is not creating changes in the original hash because this method is an example of non-destructive methods where the changes created by the method are not permanent.

在上面的代码中,您可以观察到可以借助Hash.rehash()方法重新哈希一个哈希对象。 我们在哈希表中插入了一个新元素,并替换了当前元素。 重新哈希哈希实例后,我们得到了结果。 此方法不会在原始哈希中创建更改,因为此方法是非破坏性方法的示例,在该示例中,该方法创建的更改不是永久的。

Example 2:

范例2:

=begin
Ruby program to demonstrate Hash.rehash method
=end
a = ["Satyam","Amisha"]
b = ["Nikhil","Saksham"]
hsh = {a=>"friends", b=>"friends"}
puts "Hash elements are: #{hsh}"
puts "Hash.rehash implementation"
hsh.each do |key,value| hsh.rehash end
puts "Hash after rehash: #{hsh.rehash}"

Output

输出量

Hash elements are: {["Satyam", "Amisha"]=>"friends", ["Nikhil", "Saksham"]=>"friends"}
Hash.rehash implementation
rehash during iteration
(repl):14:in `rehash'
(repl):14:in `block in <main>'
(repl):14:in `each'
(repl):14:in `<main>'

Explanation:

说明:

In the above code, you can observe that when we are trying to rehash the hash instance during iteration then the method is throwing an exception named RuntimeError. This simply shows that you can’t rehash a hash during the process of iteration.

在上面的代码中,您可以观察到,当我们尝试在迭代过程中重新哈希该哈希实例时,该方法将抛出一个名为RuntimeError的异常。 这仅表明您无法在迭代过程中重新哈希。

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

ruby hash方法

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

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

相关文章

面试突击63:distinct 和 group by有什么区别?

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;在 MySQL 中&#xff0c;最常见的去重方法有两个&#xff1a;使用 distinct 或使用 group by&#xff0c;那它们有什…

从20s优化到500ms,我用了这三招

前言接口性能问题&#xff0c;对于从事后端开发的同学来说&#xff0c;是一个绕不开的话题。想要优化一个接口的性能&#xff0c;需要从多个方面着手。本文将接着接口性能优化这个话题&#xff0c;从实战的角度出发&#xff0c;聊聊我是如何优化一个慢查询接口的。上周我优化了…

camelcase_在Python中将字符串转换为camelCase

camelcaseHere, we are implementing a python program to convert a given string to camelCase. 在这里&#xff0c;我们正在实现一个python程序&#xff0c;将给定的字符串转换为camelCase。 Example of camelCase: camelCase的示例&#xff1a; String: "Hello worl…

面试拆解:系统上线后CPU急速飙升,该怎么排查?

上次面试官问了个问题&#xff1a;应用上线后Cpu使用率飙升如何排查&#xff1f;其实这是个很常见的问题&#xff0c;也非常简单&#xff0c;那既然如此我为什么还要写呢&#xff1f;因为上次回答的时候我忘记将线程PID转换成16进制的命令了。所以我决定再重温一遍这个问题&…

提高Python运行效率的六个窍门

Python性能优化的20条建议 http://segmentfault.com/a/1190000000666603优化算法时间复杂度 算法的时间复杂度对程序的执行效率影响最大&#xff0c;在Python中可以通过选择合适的数据结构来优化时间复杂度&#xff0c;如list和set查找某一个元素的时间复杂度分别是O(n)和O(1)。…

ruby hash方法_Hash.fetch()方法以及Ruby中的示例

ruby hash方法Hash.fetch()方法 (Hash.fetch() Method) In this article, we will study about Hash.fetch() 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 wi…

MySQL 死锁了,怎么办?

作者&#xff1a;小林coding提纲如下&#xff1a;正文有个业务主要逻辑就是新增订单、修改订单、查询订单等操作。然后因为订单是不能重复的&#xff0c;所以当时在新增订单的时候做了幂等性校验&#xff0c;做法就是在新增订单记录之前&#xff0c;先通过 select ... for upda…

lcfirst_PHP lcfirst()函数与示例

lcfirstPHP lcfirst()函数 (PHP lcfirst() function) lcfirst() function is a string function, it is used to convert first character to lowercase. It accepts string and returns string with first lowercase character. lcfirst()函数是一个字符串函数&#xff0c;用于…

在notepad++中运行python代码

#在notepad中运行python代码1、安装插件pyNPP&#xff0c; 2、允许插件pyNPP中的第一个和第二个选项即可&#xff0c;如果代码过少代码执行一闪而过&#xff0c;可能无法看到&#xff0c;可加入少量sleep时间即可 方法二&#xff1a;1、安装插件NppExec2、打开NppExec--Execute…

10 张图搞懂服务注册发现机制

在微服务架构或分布式环境下&#xff0c;服务注册与发现技术不可或缺&#xff0c;这也是程序员进阶之路必须要掌握的核心技术之一&#xff0c;本文通过图解的方式带领大家轻轻松松掌握。引入服务注册与发现组件的原因先来看一个问题&#xff0c;假如现在我们要做一个商城项目&a…

c# datetime._C#| DateTime.GetHashCode()方法与示例

c# datetime.DateTime.GetHashCode()方法 (DateTime.GetHashCode() Method) DateTime.GetHashCode() method is used get the 32-bit signed integer hash code of DateTime class object. DateTime.GetHashCode()方法用于获取DateTime类对象的32位带符号整数哈希码。 Syntax:…

ASP.NET 5 Beta8 已经发布

Microsoft ASP.NET and Web Tools 2015 (Beta8) http://www.microsoft.com/en-us/download/details.aspx?id49442 .net core 完成了98%&#xff0c;绝大部分类库完成了跨平台开发&#xff0c;已经基本可用&#xff0c;下一版本为RC&#xff0c;发布时间为12月&#xff0c;将可…

面试突击65:HTTPS有什么优点?说一下它的执行流程?

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;说到 HTTPS 相信大部分人都是不陌生&#xff0c;因为目前我们使用的绝大数网站都是基于 HTTPS 的&#xff0c;比如以…

nanf flash校验_C ++中带有示例的nanf()函数

nanf flash校验C Nanf()函数 (C nanf() function) nanf() function is a library function of cmath header, it is used to get the NaN value of type float. It accepts an argument (which is an implementation-specific C String – to get NaN value we have to pass a…

Cell.reuseIdentifier 指什么

Cell.reuseIdentifier 指的是 默认为空&#xff0c;如果不定义&#xff0c;在执行 [_tableView registerNib:templateCellNib forCellReuseIdentifier:_templateCell.reuseIdentifier]; 时&#xff0c;提示 must pass a valid reuse identifier to -[UITableView registerNib:f…

缓存穿透、缓存雪崩、缓存击穿?

背景 在现代软件架构中&#xff0c;缓存的应用已经非常普及。缓存的使用在面试和实践中都是避不开的硬技能、硬知识&#xff0c;如果你说还不太熟悉缓存的使用&#xff0c;可能都不好意思说自己是程序员。这篇文章&#xff0c;带大家进一步学习在缓存使用中不得不考虑三个特殊场…

c语言 div ldiv_C ++中带有示例的ldiv()函数

c语言 div ldivC ldiv()函数 (C ldiv() function) ldiv() function is a library function of cstdlib header. It is used for integral division, it accepts two parameters (numerator and denominator) and returns a structure that contains the quot (quotient) and r…

网盘搜索

http://pan.java1234.com1、在http://baidu.com的搜索框中输入&#xff1a;site:http://pan.baidu.com 搜索词&#xff08;回复中林涛同学建议第一条如果用谷歌搜site:百度网盘的话效果会好一点&#xff09;2、壹搜 网盘搜索引擎3、盘易搜 盘易搜-百度网盘搜索4、BD盘搜索 百度…

如何防止订单重复支付?

大家好&#xff0c;我是磊哥&#xff0c;想必大家对在线支付都不陌生&#xff0c;今天和大家聊聊如何防止订单重复支付。看看订单支付流程我们来看看&#xff0c;电商订单支付的简要流程&#xff1a;订单钱包支付流程从下单/计算开始&#xff1a;下单/结算&#xff1a;这一步虽…

c ++atoi函数_atoi()函数以及C ++中的示例

c atoi函数C atoi()函数 (C atoi() function) atoi() function is a library function of cstdlib header. It is used to convert the given string value to the integer value. It accepts a string containing an integer (integral) number and returns its integer valu…