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方法

ruby hash方法_Ruby中带有示例的Hash.rehash方法相关推荐

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

    ruby hash方法 Hash.invert方法 (Hash.invert Method) In this article, we will study about Hash.invert Meth ...

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

    ruby hash方法 哈希选择方法 (Hash.select Method) In this article, we will study about Hash.select Method. The ...

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

    ruby hash方法 哈希平化方法 (Hash.flatten Method) In this article, we will study about Hash.flatten Method. T ...

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

    ruby hash方法 哈希值方法 (Hash.values Method) In this article, we will study about Hash.values Method. The ...

  5. ruby hash方法_Ruby中带有示例的Hash.default(key = nil)方法

    ruby hash方法 Hash.default(key = nil)方法 (Hash.default(key=nil) Method) In this article, we will study ...

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

    ruby hash方法 哈希长度方法 (Hash.length Method) In this article, we will study about Hash.length Method. The ...

  7. ruby hash方法_Ruby中带有示例的Hash.key?(obj)方法

    ruby hash方法 Hash.key?(obj)方法 (Hash.key?(obj) Method) In this article, we will study about Hash.key?( ...

  8. ruby hash方法_Ruby中带有示例的Hash.rassoc(obj)方法

    ruby hash方法 Hash.rassoc(obj)方法 (Hash.rassoc(obj) Method) In this article, we will study about Hash.r ...

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

    ruby hash方法 哈希键方法 (Hash.keys Method) In this article, we will study about Hash.keys Method. The work ...

最新文章

  1. python图片-Python图像处理
  2. 028_jdbc-mysql大文本
  3. 2 了解MyBatis配置文件
  4. Netflix是如何构建代码的
  5. HDU1812 - Count the Tetris
  6. 前端学习(2996):vue+element今日头条管理--代码测试规范
  7. Linux学习笔记-生成动态库(补充说明)
  8. IDEA、Eclipse的详细安装配置及Tomcat服务集成介绍
  9. C# 利用反射动态创建对象[摘录]
  10. 使用Python批量替换指定目录所有文件中的指定文本
  11. drools规则引擎中易混淆语法分析_相互触发导致死循环分析
  12. vi 编辑器基本命令
  13. ECMAScript版本号总结
  14. 【latex论文】IEEE论文模板的使用教程
  15. 世界杯winner只属于你——MESSI
  16. c#MVC文件(图片,word,excel,zip等)批量上传
  17. 远程破解Linux操作系统密码
  18. 幻影坦克制作流程和步骤
  19. 嵌入式stm32项目开发 心率检测仪的设计与实现
  20. 安装声卡驱动后还是没声音?Win10如何区分新旧面板

热门文章

  1. android 实例源码解释,Android Handler 原理分析及实例代码
  2. mongotemplate中save抛出异常_异常处理的三个好习惯 | Python 工匠
  3. Php公钥加密data是空,php 生成加密公钥加密私钥实例详解
  4. epson彩色打印机加墨水_爱普生打印机墨盒如何加墨?
  5. Nginx的执行阶段详解
  6. 趣图:当我捕获Bug的时候
  7. mysql 5.6.4以上版本innodb支持全文索引的测试
  8. esp32的GPIO操作
  9. Swift iOS : 内存管理
  10. iOS开发中 常用枚举和常用的一些运算符(易错总结)