加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – Yaml输出填充!map:ActiveSupport :: HashW

发布时间:2020-12-17 03:00:49 所属栏目:百科 来源:网络整理
导读:我升级到rails 3.0.9并且有一个新的yaml gem.不幸的是,to_yaml输出相当大的哈希的方式已经发生了某种变化.现在,我得到了很多“!map:ActiveSupport :: HashWithIndifferentAccess”之前我没有得到的元素,据我所知,这是一个bug,因为我试图使我的.yml尽可能易
我升级到rails 3.0.9并且有一个新的yaml gem.不幸的是,to_yaml输出相当大的哈希的方式已经发生了某种变化.现在,我得到了很多“!map:ActiveSupport :: HashWithIndifferentAccess”之前我没有得到的元素,据我所知,这是一个bug,因为我试图使我的.yml尽可能易读.有没有办法摆脱yaml输出中的“!map:ActiveSupport :: HashWithIndifferentAccess”?它不会影响它加载到我的代码中的方式,所以我没有看到在输出中有它的点.我可以把它搞定,但我认为这里还有其他一些我不知道的事情.

典型的哈希输入:

{"All" => 
  {"A test" => {"string"=>"This is just a test","description" => "This is a description for a test string","alternatives" => [{"new" => "woot"}]}}

Yaml输出:

All:
  A test: !map:ActiveSupport::HashWithIndifferentAccess 
    string: This is just a test
    description: This is a description for a test string
    alternatives:
    - !map:ActiveSupport::HashWithIndifferentAccess 
      new: woot

我想要的(以及我以前得到的):

All:
  A test:
    string: This is just a test
    description: This is a description for a test string
    alternatives:
      - new: woot

注意:我的输出必须是UTF-8.

解决方法

我怀疑你的哈希来自控制器中的params,其类型为ActiveSupport :: HashWithIndifferentAccess.修复输出的一种方法是通过猴子修补其to_yaml方法,在将其转换为YAML之前将其自身转换为普通哈希.

# config/initializers/hash_with_indifferent_access.rb
class HashWithIndifferentAccess < Hash
  def to_yaml(opts = {})
    self.to_hash.to_yaml(opts)
  end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读