如何在 yaml 中存储带有嵌入变量的 ruby 字符串,但仅在从 yaml 获取字符串时才插入变量值?
问问题
5446 次
2 回答
19
str = "Hi %{name}, %{msg}. Bye %{name}." #yaml it, de-yaml it back to string
h = {:name=> "John", :msg=> "this message is for you"}
puts str % h
#=>Hi John, This message is for you. Bye John.
于 2012-05-31T14:16:40.367 回答
2
像 rails 一样将 erb 嵌入到 yaml 文件中:
# config/initializers/load_config.rb
APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")[Rails.env]
# config/app_config.yml
development:
key1: <%= # ruby code ... %>
test:
key1: <%= # ruby code ... %>
production:
key1: <%= # ruby code ... %>
于 2012-05-31T14:25:14.760 回答