博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ruby hash方法_Ruby中带有示例的Hash.invert方法
阅读量:2528 次
发布时间:2019-05-11

本文共 3091 字,大约阅读时间需要 10 分钟。

ruby hash方法

Hash.invert方法 (Hash.invert Method)

In this article, we will study about Hash.invert 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 with the help of its syntax and program code in the rest of the content.

在本文中,我们将研究Hash.invert方法 。 可以借助其名称来预测此方法的工作,但是它并不像看起来那样简单。 好了,我们将在其余内容中借助其语法和程序代码来理解此方法。

Method description:

方法说明:

This method is a public instance method that is defined in Ruby library especially for Hash class. This method works in a way that it creates a new hash object in which the keys of the self hash are values and values of the self hash are keys. If a key with the same values already exists in the hash object then the last key will be utilized and the previous one will be discarded. This method is one of the examples of non-destructive methods, where the changes created by the method are temporary.

此方法是在Ruby库中定义的公共实例方法,特别是针对Hash类。 此方法的工作方式是创建一个新的哈希对象,其中自哈希的键是值,而自哈希的值是键。 如果散列对象中已经存在具有相同值的键,则将使用最后一个键,而前一个键将被丢弃。 此方法是非破坏性方法的示例之一,其中该方法创建的更改是临时的。

Syntax:

句法:

Hash_object.invert

Argument(s) required:

所需参数:

This method does not require any argument.

此方法不需要任何参数。

Example 1:

范例1:

=begin  Ruby program to demonstrate invert method=end	hash1={
"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}puts "Hash.invert implementation"ary = hash1.invertputs "Hash object after inverting: #{ary}"puts "Self hash object : #{hash1}"

Output

输出量

Hash.invert implementationHash object after inverting: {"Black"=>"color", "car"=>"object", "friends"=>"love", "Kiwi"=>"fruit", "potato"=>"vege"}Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

说明:

In the above code, you can observe that we are inverting the hash object with the help of the Hash.invert method. In the new hash, the keys are values and values are keys. You can see that this method is not creating any impact upon the actual hash because this method is one of the examples of non-destructive methods.

在上面的代码中,您可以观察到我们在Hash.invert方法的帮助下反转了哈希对象。 在新的哈希中,键是值,而值是键。 您可以看到该方法不会对实际哈希产生任何影响,因为该方法是非破坏性方法的示例之一。

Example 2:

范例2:

=begin  Ruby program to demonstrate invert method=end	hash1={
"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}puts "Hash.invert implementation"ary = hash1.invert.invertputs "Hash object after inverting: #{ary}"puts "Self hash object : #{hash1}"

Output

输出量

Hash.invert implementationHash object after inverting: {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

说明:

In the above code, you can observe that we can invert a hash object with the help of the Hash.invert method. You can even see that we can do cascading of methods and we are getting the same hash after invoking invert twice.

在上面的代码中,您可以观察到我们可以借助Hash.invert方法反转哈希对象。 您甚至可以看到我们可以级联方法,并且调用两次invert后得到相同的哈希值。

翻译自:

ruby hash方法

转载地址:http://zqxzd.baihongyu.com/

你可能感兴趣的文章
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>
i++和++1
查看>>
react.js
查看>>
P1313 计算系数
查看>>
NSString的长度比较方法(一)
查看>>
Azure云服务托管恶意软件
查看>>
My安卓知识6--关于把项目从androidstudio工程转成eclipse工程并导成jar包
查看>>