# Probs with accessing variables in yaml file

**URL:** <https://www.ruby-forum.com/t/probs-with-accessing-variables-in-yaml-file/104089>\
**Category:** Ruby\
**Created:** [May 14, 2007, 8:55am UTC](https://www.ruby-forum.com/t/probs-with-accessing-variables-in-yaml-file/104089 "2007-05-14T08:55:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RebhanS\_Gilbert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@RebhanS\_Gilbert](https://www.ruby-forum.com/u/RebhanS_Gilbert)\
**Post date:** [May 14, 2007, 8:55am UTC](https://www.ruby-forum.com/t/probs-with-accessing-variables-in-yaml-file/104089/1 "2007-05-14T08:55:13Z")

</div>

Hi,

i have a script that is configured via yaml file :

—script-----------

require ‘yaml’  
require ‘highline/import’  
require ‘win32/registry’

config=YAML.load\_file(“cvs\_login.yaml”)

[…]

def filesed!(pattern, replaceString, filename)  
regexPattern = Regexp.compile(pattern)  
tmpFilename = “#{filename}.#{Time.now.usec}”  
tmpFile = File.new(tmpFilename, “w”)  
srcFile = File.open(filename)  
srcFile.each\_line do |line|  
tmpFile.print line.gsub(regexPattern) {|match|  
replaceString  
}  
end  
tmpFile.close  
srcFile .close  
File.rename(tmpFilename, filename)  
end

if config[‘targetdirs’]

Win32::Registry::HKEY\_CURRENT\_USER.open(‘Software\Cvsnt\cvspass’) do  
|reg|  
@cvsreg =  
reg[":pserver:#{ENV[“USERNAME”]}@cvsprod:d:/cvsrepos/test"]  
end

config[‘targetdirs’].each do |dir|  
Dir.chdir(dir)  
puts “\n\n”  
Dir.glob(config[‘targetfilepattern’]).each do |file|  
puts “… processing “\<\<File.expand\_path(”#{file}”)  
puts ‘bla’\<\<@cvsreg  
puts 'replaceto == '\<\<config[‘replaceto’]

#filesed!(".\*\</Passwort\>","\</Pa  
sswort\>","#{file}")  
filesed!(config[‘replacefrom’],config[‘replaceto’],"#{file}")  
end  
end  
puts “\n\nDone !!”  
sleep 1  
else  
puts “\n\nDone !!”  
sleep 1  
end

—script-----------

–yaml-------------

cvs\_login.yaml looks like =

targetdirs:

- Y:/tempwork

targetfilepattern: “Scm\*.xml”

replacefrom: “.\*\</Passwort\>”  
replaceto: “\<![CDATA[#{@cvsreg}]\>\</Passwort\>”

–yaml--------------

Question =

If i write :  
puts 'replaceto == '\<\<"\</Passwort\>"  
direct into the script it’s echoed correct, but it doesn’t work via yaml  
file;  
when running via yaml the line in my xml file get’s replaced to =

\<![CDATA[#{@cvsreg}]\>\</Passwort\>

Where’s the failure ??

Regards, Gilbert

---

<div class="post-metadata">

**Author:** ![RebhanS\_Gilbert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@RebhanS\_Gilbert](https://www.ruby-forum.com/u/RebhanS_Gilbert)\
**Post date:** [May 14, 2007, 11:30am UTC](https://www.ruby-forum.com/t/probs-with-accessing-variables-in-yaml-file/104089/2 "2007-05-14T11:30:38Z")

</div>

Hi,

don’t know if there’s another | better way, but  
i solved it like that =

yaml has now =

replaceto1: “\<![CDATA[”  
replaceto2: “]\>”

and my script has =

s=config[‘replaceto1’]\<\<"#{@cvsreg}"\<\<config[‘replaceto2’]  
[…]  
filesed!(config[‘replacefrom’],s,"#{file}")

Gilbert

---

<div class="post-metadata">

**Author:** ![RebhanS\_Gilbert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@RebhanS\_Gilbert](https://www.ruby-forum.com/u/RebhanS_Gilbert)\
**Post date:** [May 14, 2007, 11:57am UTC](https://www.ruby-forum.com/t/probs-with-accessing-variables-in-yaml-file/104089/3 "2007-05-14T11:57:41Z")

</div>

On 5/14/07, Rebhan, Gilbert [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> replaceto2: “]\>”
> 
> and my script has =
> 
> s=config[‘replaceto1’]\<\<“#{@cvsreg}”\<\<config[‘replaceto2’]  
> […]  
> filesed!(config[‘replacefrom’],s,“#{file}”)

Hi,

the #{} are evaluated when the string is instantiated, i.e. in normal  
case when the line containing the literal (note: the LITERAL) is  
evaluated. When you load the string from yaml, these #{} are not  
evaluated at all.

What you might want to do is to add quotes around and call eval –  
e.g. config[‘replaceto’] = eval(“"#{config[‘replaceto’]}"”), with  
all the dangerous implications of eval. (Note the double use of #{}…  
once it is evaluated as the line is parsed to create another literal  
that is then passed to eval to expand again. I could have avoided the  
outer expansion by using “"” + config[…] + “"”, but I guess this  
one’s faster), and it’s shorter in any case.)
