Home > OS >  How to generate .yml file from Ruby on Rails?
How to generate .yml file from Ruby on Rails?

Time:12-15

I would like generate the following result in a .yml file from Ruby on Rails:

include:
  - template: ./my_folder/my_file.txt

But I am generating the following result with single quote (''):

include:
- 'template: ./my_folder/my_file.txt'

I have tried generate the .yml file with this ruby code:

my_variable = {"include" => ["template: ./my_folder/my_file.txt"]}

CodePudding user response:

I would do this:

my_variable = { "include" => [{ "template" => "./my_folder/my_file.txt" }] }
my_variable.to_yaml
# ---
# include:                                
# - template: "./my_folder/my_file.txt"   

CodePudding user response:

my_variable = {"include" => ["template: ./my_folder/my_file.txt"]}
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The Array contains a String, not a Hash. It will produce a YAML array with a single string value, not an array with a key/value pair.

Either you have a typo in your data, or you need to split the values in the Array on : to produce a Hash.

  • Related