Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个 ruby 具有一对一的对应关系,是否有一些内置方法可以反转 ruby 哈希中的关联?我宁愿在不显式循环键的情况下这样做。
例如,假设我有:
a = {1 => "Foo", 2 => "Bar"} a.reverse_association a # ---> {"Foo" => 1, "Bar" => 2}
是的,使用Hash#invert:
Hash#invert
h = {a: 1, b: 2} h.invert #=> {1 => :a, 2 => :b}