YAML & String Interpolation

I’m trying to read in a YAML string stored in a configuration file and
have
Ruby perform String interpolation on it.

For example,
=== YAML file ===
remote_machine: “${server}:${port}”

=== RUBY ===

def load_settings
@options = {}
imports = YAML.load_file(‘server-info.yaml’)
if imports
imports.each_pair do |key, value|
if not key.is_a? Symbol
imports[key.to_sym] = value
imports.delete(key)
end
end
@options = imports.merge(@options)
end
end

server = ‘192.168.0.1’
port = ‘8080’
print @options[:remote_machine] # => 192.168.0.1:8080

Instead what I’m getting is the printing of ${server}:${port}.

On Sep 29, 2009, at 08:55 , Chris D. wrote:

I’m trying to read in a YAML string stored in a configuration file
and have
Ruby perform String interpolation on it.

yaml doesn’t interpolate on load_file (or any method). You’ll want to
do that yourself. I suggest you use ERB. There are plenty of snippets
around you can snipe for that (hoe’s bin/sow has one, so does rails
tho that is quite buried).

On Sep 29, 11:55 am, Chris D. [email protected] wrote:

@options = {}

end

server = ‘192.168.0.1’
port = ‘8080’
print @options[:remote_machine] # => 192.168.0.1:8080

Instead what I’m getting is the printing of ${server}:${port}.

require ‘facets/string/interpolate’
server = ‘192.168.0.1’
String.interpolate{’#{server}’}

However, I would recommend ERB for this usecase too.

T.

Thanks folks. I’ll give ERb a shot.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs