YAML.load 在 Rails 初始值设定项中永远不会返回

发布于 2025-01-07 07:30:21 字数 1287 浏览 1 评论 0原文

我试图在 Rails 3.1 应用程序初始化期间加载 yaml 配置文件,并且对 YAML.load 的调用永远不会返回。这是我的初始化程序文件:

STRIPE_CONFIG = begin
  config = YAML.load(Rails.root.join('config', 'stripe.yml')) || {}
  config = config[Rails.env] || {}
  config.to_options
end

这是我的 stripe.yml 文件:

default: &default
  api_key:    test
  public_key: test

development:
  <<: *default

test:
  <<: *default

production:
  api_key:    prod
  public_key: prod

无论出于何种原因,YAML.load 调用永远不会返回。如果我执行堆栈跟踪,它似乎卡在 syck.rb 第 135 行上。有趣的是,我让应用程序在中断之前停留的时间越长,对第 135 行的调用就越多。

/Users/mhuggins/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/syck.rb:135:in `read'
/Users/mhuggins/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/syck.rb:135:in `read'
/Users/mhuggins/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/syck.rb:135:in `load'
/Users/mhuggins/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/syck.rb:135:in `load'
/Users/mhuggins/Sites/dating/config/initializers/stripe.rb:2:in `<top (required)>'
...

我也尝试过明确使用 Psych 而不是使用 Syck,但没有运气。 (它最终也会挂起。)

STRIPE_CONFIG = begin
  require 'psych'
  config = Psych.load(Rails.root.join('config', 'stripe.yml')) || {}
  config = config[Rails.env] || {}
  config.to_options
end

I'm trying to load a yaml config file during initialization of my Rails 3.1 app, and the call to YAML.load never returns. Here is my initializer file:

STRIPE_CONFIG = begin
  config = YAML.load(Rails.root.join('config', 'stripe.yml')) || {}
  config = config[Rails.env] || {}
  config.to_options
end

And here is my stripe.yml file:

default: &default
  api_key:    test
  public_key: test

development:
  <<: *default

test:
  <<: *default

production:
  api_key:    prod
  public_key: prod

For whatever reason, the YAML.load call never returns. If I perform a stack trace, it seems to be stuck on syck.rb line 135. The interesting thing is, the longer I let my app sit before breaking, the more calls to line 135 appear.

/Users/mhuggins/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/syck.rb:135:in `read'
/Users/mhuggins/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/syck.rb:135:in `read'
/Users/mhuggins/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/syck.rb:135:in `load'
/Users/mhuggins/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/syck.rb:135:in `load'
/Users/mhuggins/Sites/dating/config/initializers/stripe.rb:2:in `<top (required)>'
...

I've tried explicitly using Psych as well instead of using Syck, but with no luck. (It ends up hanging as well.)

STRIPE_CONFIG = begin
  require 'psych'
  config = Psych.load(Rails.root.join('config', 'stripe.yml')) || {}
  config = config[Rails.env] || {}
  config.to_options
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南烟 2025-01-14 07:30:21

答案会有点晚,但我刚才偶然发现了类似的问题;)

您可以使用 YAML::load_file ,它需要文件名作为参数。

Answer will be kinda late, but I have stumbled upon similar problem just now ;)

You could use YAML::load_file, which expects filename as an argument.

叹梦 2025-01-14 07:30:21

呃,显然我只需要显式读取该文件。我把这个: 改为

YAML.load(Rails.root.join('config', 'stripe.yml'))

YAML.load(File.open(Rails.root.join('config', 'stripe.yml')))

Ugh, apparently I just needed to explicitly read the file. I changed this:

YAML.load(Rails.root.join('config', 'stripe.yml'))

to this:

YAML.load(File.open(Rails.root.join('config', 'stripe.yml')))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文