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

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, 2009, at 08:55 , Chris Dempsey wrote:

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

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

However, I would recommend ERB for this usecase too.

T.

···

On Sep 29, 11:55 am, Chris Dempsey <cdal...@gmail.com> wrote:

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

Thanks folks. I'll give ERb a shot.

···

On Tue, Sep 29, 2009 at 4:21 PM, trans <transfire@gmail.com> wrote:

On Sep 29, 11:55 am, Chris Dempsey <cdal...@gmail.com> wrote:
> 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.