Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby executable in YAML

I have a sample yaml file(abc.yaml) like this:

entity1:
  condition: "created_at >= #{Date.today - 3.months}"

I want to read this condition from YAML in Ruby as :

"created_at >= 2015-03-02"

But when I do

YAML.load_file('abc.yaml')["entity1"]["condition"]

I get:

"created_at >= \#{Date.today - 3.months}"

Please let me know how to work around this.

like image 641
Saurabh Avatar asked Sep 18 '26 08:09

Saurabh


2 Answers

The safer option would be to only convert known data from a pre-determined format. Or use something cool like settingslogic https://github.com/settingslogic/settingslogic. It uses ERB-style formatting to load settings, so you get some built in safety.

eg:

defaults: &defaults
  cool:
    saweet: nested settings
  neat_setting: 24
  awesome_setting: <%= "Did you know 5 + 5 = #{5 + 5}?" %>
like image 173
Rots Avatar answered Sep 19 '26 21:09

Rots


Do this only if you're absolutely sure nobody can change your yaml file to inject something harmful:

condition = YAML.load_file('abc.yaml')["entity1"]["condition"]
condition = eval "\"#{condition}\""
like image 31
makhan Avatar answered Sep 19 '26 22:09

makhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!