当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Ruby Hash.invert用法及代码示例

本文简要介绍ruby语言中 Hash.invert 的用法。

用法

invert → new_hash

返回每个键值对反转的新 Hash 对象:

h = {foo: 0, bar: 1, baz: 2}
h1 = h.invert
h1 # => {0=>:foo, 1=>:bar, 2=>:baz}

覆盖任何重复的新键:(参见入场顺序):

h = {foo: 0, bar: 0, baz: 0}
h.invert # => {0=>:baz}

相关用法


注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Hash.invert。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。