Ruby-on-rails 如何将哈希键从“符号”更改为“字符串”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10549554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:23:19  来源:igfitidea点击:

How to change hash keys from `Symbol`s to `String`s?

ruby-on-railsrubyruby-on-rails-3hash

提问by user12882

I am using Ruby on Rails 3.2.2 and I would like to "easily" / "quickly" change hash keys from Symbols to Strings. That is, from {:one => "Value 1", :two => "Value 2", ...}to {"one" => "Value 1", "two" => "Value 2", ...}.

我正在使用 Ruby on Rails 3.2.2,我想“轻松”/“快速”将哈希键从Symbols更改为Strings。也就是说,从{:one => "Value 1", :two => "Value 2", ...}{"one" => "Value 1", "two" => "Value 2", ...}

How can I make that by using less code as possible?

我怎样才能通过尽可能少的代码来做到这一点?

回答by Viktor Trón

simply call stringify_keys(or stringify_keys!)

只需调用stringify_keys(或stringify_keys!

http://apidock.com/rails/Hash/stringify_keys

http://apidock.com/rails/Hash/stringify_keys

回答by jdoe

Use stringify_keys/stringify_keys!methods of the Hashclass.

使用stringify_keys/类的stringify_keys!方法Hash

You can also use some_hash.with_indifferent_accessto return a Hash instance where your key can be specified as symbols or as strings with no difference.

您还可以使用some_hash.with_indifferent_access返回一个 Hash 实例,其中您的键可以指定为符号或字符串,没有区别。

回答by eli

stringify_keysis nice, but only available in Rails. Here's how I would do it in a single line, with zero dependencies:

stringify_keys很好,但仅在 Rails 中可用。下面是我将如何在一行中完成它,零依赖:

new_hash = Hash[your_hash.collect{|k,v| [k.to_s, v]}]

This works on Ruby 1.8.7 and up. If you are working with Ruby 2.1, you can do:

这适用于 Ruby 1.8.7 及更高版本。如果您使用 Ruby 2.1,您可以执行以下操作:

new_hash = a.collect{|k,v| [k.to_s, v]}.to_h

Note that this solution is not recursive, nor will it handle "duplicate" keys properly. eg. if you have :keyand also "key"as keys in your hash, the last one will take precedence and overwrite the first one.

请注意,此解决方案不是递归的,也不会正确处理“重复”键。例如。如果您有:key和 也"key"作为哈希中的键,则最后一个将优先并覆盖第一个。

回答by Ery

stringify_keys from rails

来自 rails 的 stringify_keys

http://api.rubyonrails.org/classes/Hash.html#method-i-stringify_keys

http://api.rubyonrails.org/classes/Hash.html#method-i-stringify_keys

hash = { name: 'Rob', age: '28' }
hash.stringify_keys
# => { "name" => "Rob", "age" => "28" }

回答by rik.vanmechelen

there is a nice library that does the trick, the library is "facets/hash/rekey" and the method is rekey!. Se my example below of how to use it. It is just a copy past of

有一个很好的库可以解决这个问题,库是“facets/hash/rekey”,方法是rekey!。下面是我如何使用它的示例。这只是一个复制过去

> require 'facets/hash/rekey'
 => true
> a = {:one => "Value 1", :two => "Value 2"}
 => {:one=>"Value 1", :two=>"Value 2"} 
> a.rekey!(&:to_s)
 => {"one"=>"Value 1", "two"=>"Value 2"} 
> a
 => {"one"=>"Value 1", "two"=>"Value 2"}

回答by abhas

 new_hash = Hash.new
 your_hash.each{ |k,v| new_hash[k.to_s] = v }

new_hash will be same as your_hash but with string keys

new_hash 将与 your_hash 相同,但带有字符串键

回答by pguardiario

I came here to see if there was something better than:

我来这里是为了看看是否有比以下更好的东西:

JSON.parse(hash.to_json)

But I think I'll stick with what I have.

但我想我会坚持我所拥有的。

回答by kjian

You can transfer the key from symbols to strings explicitly:
hash = hash.map { |k, v| [k.to_s, v] }.to_h

您可以显式地将键从符号转移到字符串:
hash = hash.map { |k, v| [k.to_s, v] }.to_h

回答by mooreds

hash = hash.transform_keys(&:to_s)turns all keys from symbols into strings.

hash = hash.transform_keys(&:to_s)将所有键从符号转换为字符串。

More here: https://ruby-doc.org/core-2.6.3/Hash.html#method-i-transform_keys

更多信息:https: //ruby-doc.org/core-2.6.3/Hash.html#method-i-transform_keys

This was added in ruby 2.5: https://bugs.ruby-lang.org/issues/13583

这是在 ruby​​ 2.5 中添加的:https: //bugs.ruby-lang.org/issues/13583