Ruby on Rails:你能把 Ruby 代码放在 YAML 配置文件中吗? [英] Ruby on Rails: Can you put Ruby code in a YAML config file?

查看:37
本文介绍了Ruby on Rails:你能把 Ruby 代码放在 YAML 配置文件中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 amazon_s3.yml 配置文件中做这样的事情:

I would like to do something like this in my amazon_s3.yml config file:

access_key_id: ENV['S3_KEY']
secret_access_key: ENV['S3_SECRET']

...但我知道这行不通.不确定是否有可能,但是您可以将 Ruby 代码放入 YAML 文件中吗?

...but I know this isn't working. Not sure if it is even possible, but can you put Ruby code in a YAML file?

推荐答案

不正常/直接.我这样说是因为为了使用 ruby​​ 结果,你需要在加载文件之前先使用类似 ERB 的东西.在代码方面,您需要从以下内容出发:

Not normally / directly. I say this because in order to use ruby results you need to use something like ERB first before loading the file. In terms of code, you need to go from something like:

loaded_data = YAML.load_file("my-file.yml")

甚至

loaded_data = YAML.load(File.read("my-file.yml"))

致:

loaded_data = YAML.load(ERB.new(File.read("my-file.yml")).result)

在这种特定情况下,您必须查看加载文件的内容 - 在某些情况下,它可能已经被设计为直接从环境中加载它,或者您可能需要:

In this specific case, you would have to look at what is loading the file - in some cases, it may already be designed to load it straight out of the environment or you may need to either:

  1. 猴子补丁代码
  2. Fork + 使用您的自定义版本.

由于有一些插件使用 amazon_s3.yml,因此可能值得发布您正在使用的库使用它 - 或者,我相信从一个快速的谷歌,AWS 库允许您将 AMAZON_ACCESS_KEY_ID 和 AMAZON_SECRET_ACCESS_KEY 定义为环境变量它会开箱即用.

Since there are a few plugins that use amazon_s3.yml, It might be worth posting which library you are using that uses it - alternatively, I believe from a quick google that the AWS library lets you define AMAZON_ACCESS_KEY_ID and AMAZON_SECRET_ACCESS_KEY as env vars and it will pick them up out of the box.

这篇关于Ruby on Rails:你能把 Ruby 代码放在 YAML 配置文件中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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