Rails 将 YAML 加载到散列并按符号引用 [英] Rails load YAML to hash and reference by symbol

查看:13
本文介绍了Rails 将 YAML 加载到散列并按符号引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rails 3.0.9 中加载一个 YAML 文件,如下所示:

I am loading a YAML file in Rails 3.0.9 like this:

APP_CONFIG = YAML.load(File.read(File.expand_path('../app.yml', __FILE__)))

它加载所有内容,如分层哈希,没问题.我不喜欢的部分是哈希只能用单引号或双引号而不是符号来访问.

It loads the all of the contents like hierarchical hashes, no problem. The part I don't like is the fact that the hashes can only be accessed with single or double quotes but not a symbol.

APP_CONFIG['mailer']['username']  # works fine
APP_CONFIG[:mailer][:username]    # doesn't

有什么想法吗?

推荐答案

尝试使用 HashWithIndifferentAccess 喜欢

APP_CONFIG = HashWithIndifferentAccess.new(YAML.load(File.read(File.expand_path('../app.yml', __FILE__))))

这篇关于Rails 将 YAML 加载到散列并按符号引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆