YAML & String Interpolation

C

Chris Dempsey

[Note: parts of this message were removed to make it a legal post.]

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}.
 
R

Ryan Davis

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).
 
T

trans

I'm trying to read in a YAML string stored in a configuration file and ha= ve
Ruby perform String interpolation on it.

For example,
=3D=3D=3D YAML file =3D=3D=3D
remote_machine: "${server}:${port}"

=3D=3D=3D RUBY =3D=3D=3D

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

server =3D '192.168.0.1'
port =3D '8080'
print @options[:remote_machine] # =3D> 192.168.0.1:8080

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

require 'facets/string/interpolate'
server =3D '192.168.0.1'
String.interpolate{'#{server}'}

However, I would recommend ERB for this usecase too.

T.
 
C

Chris Dempsey

[Note: parts of this message were removed to make it a legal post.]

Thanks folks. I'll give ERb a shot.

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}.

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

However, I would recommend ERB for this usecase too.

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top