日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/11 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby hash方法_Ruby中带有示例的Hash.rehash方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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方法 。 由于該方法的名稱完全不同,因此無(wú)法進(jìn)行假設(shè)。 讓我們閱讀其定義并在語(yǔ)法和程序代碼的幫助下了解其實(shí)現(xiàn)。

Method description:

方法說(shuō)明:

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實(shí)例方法,屬于Hash類,它位于Ruby語(yǔ)言庫(kù)中。 此方法以構(gòu)造新哈希對(duì)象的方式工作,該對(duì)象基于單個(gè)鍵的當(dāng)前哈希對(duì)象值。 如果聲明了哈希對(duì)象后引入了更改,則此方法將對(duì)哈希中的鍵進(jìn)行重新索引。 如果在遍歷哈希實(shí)例的過(guò)程中嘗試調(diào)用此函數(shù),則將面臨運(yùn)行時(shí)錯(cuò)誤。

Syntax:

句法:

Hash_object.rehash

Argument(s) required:

所需參數(shù):

This method does not require any arguments.

此方法不需要任何參數(shù)。

Example 1:

范例1:

=beginRuby program to demonstrate Hash.rehash method =enda = ["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:

說(shuō)明:

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()方法重新哈希一個(gè)哈希對(duì)象。 我們?cè)诠1碇胁迦肓艘粋€(gè)新元素,并替換了當(dāng)前元素。 重新哈希哈希實(shí)例后,我們得到了結(jié)果。 此方法不會(huì)在原始哈希中創(chuàng)建更改,因?yàn)榇朔椒ㄊ欠瞧茐男苑椒ǖ氖纠?#xff0c;在該示例中,該方法創(chuàng)建的更改不是永久的。

Example 2:

范例2:

=beginRuby program to demonstrate Hash.rehash method =enda = ["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 endputs "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:

說(shuō)明:

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.

在上面的代碼中,您可以觀察到,當(dāng)我們嘗試在迭代過(guò)程中重新哈希該哈希實(shí)例時(shí),該方法將拋出一個(gè)名為RuntimeError的異常。 這僅表明您無(wú)法在迭代過(guò)程中重新哈希。

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

ruby hash方法

總結(jié)

以上是生活随笔為你收集整理的ruby hash方法_Ruby中带有示例的Hash.rehash方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。