my_overflowed_stack my_overflowed_stack - 4 months ago 10
Ruby Question

Using a global ruby variable inside YAML

Given the following YAML file:

---
foo: <%= $stdout %>


When I try to ERB this yaml file:

YAML.load(ERB.new(File.read(filename)).result)


I get the following result:

{"foo"=>nil}


Why isn't
$stdout
resolving correct? If I put other ruby code in there, it works as expected.

Answer

Look at what you get when you just run ERB:

ERB.new("foo: <%= $stdout %>").result

The result is this:

foo: #<IO:0x0056078694db38>

So foo will be set to nil because # is the comment character in YAML.