# YAML.load\_file problems

**URL:** <https://www.ruby-forum.com/t/yaml-load-file-problems/88531>\
**Category:** Ruby\
**Created:** [March 12, 2007, 12:09pm UTC](https://www.ruby-forum.com/t/yaml-load-file-problems/88531 "2007-03-12T12:09:13Z")\
**Posts on this page:** 5\
**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:** [March 12, 2007, 12:09pm UTC](https://www.ruby-forum.com/t/yaml-load-file-problems/88531/1 "2007-03-12T12:09:13Z")

</div>

Hi,

i have a program where several things are hard coded  
for production i need a config file now, must be editable  
and readable by hand, so i decided to try with YAML

program with hard coded stuff =

require “highline/import”

CVSEXE="//foobar/c$/tools/cvsnt/cvs.exe"

cvsrepos=%w[test test1 foo bar foobar]  
cvsuser = ask("Enter CVS User: ") {|q|  
q.default = “#{ENV[“USERNAME”]}”  
q.echo = true}  
cvspass = ask("Enter your password: ") { |q| q.echo = ‘\*’ }

cvsrepos.each {|x|  
puts “Login CVS Repository \>\> #{x} …”  
IO.popen("#{CVSEXE} -d  
:pserver:#{cvsuser}:#{cvspass}@cvsprod:d:/cvsrepos/#{x} login")  
}  
puts “Login successful !!”

i tried with YAML, first step =

require ‘yaml’  
require “highline/import”

CVSEXE="//foobar/c$/tools/cvsnt/cvs.exe"

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

cvsuser = ask("Enter CVS User: ") {|q|  
q.default = “#{ENV[“USERNAME”]}”  
q.echo = true}  
cvspass = ask("Enter your password: ") { |q| q.echo = ‘\*’ }

cvsrepos.each {|x|  
puts “Login CVS Repository \>\> #{x} …”  
IO.popen("#{CVSEXE} -d  
:pserver:#{cvsuser}:#{cvspass}@cvsprod:d:/cvsrepos/#{x} login")  
}  
puts “Login successful !!”

config.yaml looks like =

* * *

- test
- test1
- foo
- bar
- foobar

works fine, but now i want to get the CVSEXE path also into that  
yamlfile, i tried

require ‘yaml’  
require “highline/import”

YAML.load\_file( “config.yaml” )

cvsuser = ask("Enter CVS User: ") {|q|  
q.default = “#{ENV[“USERNAME”]}”  
q.echo = true}  
cvspass = ask("Enter your password: ") { |q| q.echo = ‘\*’ }

cvsrepos.each {|x|  
puts “Login CVS Repository \>\> #{x} …”  
IO.popen("#{CVSEXE} -d  
:pserver:#{cvsuser}:#{cvspass}@cvsprod:d:/cvsrepos/#{x} login")  
}  
puts “Login successful !!”

but that didn’t work, my config.yaml looks like =

* * *

## CVSEXE:"//foobar/c$/tools/cvsnt/cvs.exe"

cvsrepos:

- test
- test1
- foo
- bar
- foobar

What’t the correct YAML Syntax and how do i access the different  
section from my script that loads the yaml file ?

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:** [March 12, 2007, 1:08pm UTC](https://www.ruby-forum.com/t/yaml-load-file-problems/88531/2 "2007-03-12T13:08:20Z")

</div>

Alle lunedì 12 marzo 2007, Rebhan, Gilbert ha scritto:

> What’t the correct YAML Syntax and how do i access the different  
> section from my script that loads the yaml file ?

First point: your second yaml file contains a mistake: you need to put a  
space  
between the key of a hash and the value, so the second line should be

CVSEXE: “//foobar/c$/tools/cvsnt/cvs.exe”

(note the space after the 🙂

To access more than one document in a yaml file, you should use  
YAML.load\_documents. It reads the data from a String or IO object,  
parses  
each document and passes the result to the block. In your case, for  
example

File.open(“config.yaml”) do |f|  
YAML.load\_documents(f) do |d|  
p d  
end  
end

{“CVSEXE”=\>"//foobar/c$/tools/cvsnt/cvs.exe"}  
{“cvsrepos”=\>[“test”, “test1”, “foo”, “bar”, “foobar”]}

At any rate, I don’t think you need to use two yaml documents for what  
you’re  
doing. Simply use a top-level hash, with keys CVSEXE and cvsrepos:

* * *

CVSEXE: “//foobar/c$/tools/cvsnt/cvs.exe”  
cvsrepos:

- test
- test1
- foo
- bar
- foobar

YAML.load\_file(‘config.yaml’)  
=\> “CVSEXE”=\>"//foobar/c$/tools/cvsnt/cvs.exe", “cvsrepos”=\>[“test”,  
“test1”, “foo”, “bar”, “foobar”]}

I hope this helps

Stefano

---

<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:** [March 13, 2007, 9:31am UTC](https://www.ruby-forum.com/t/yaml-load-file-problems/88531/3 "2007-03-13T09:31:30Z")

</div>

Alle martedì 13 marzo 2007, Rebhan, Gilbert ha scritto:

> the shell disappears right after the input of the password, without any  
> further (error)message.  
> Hm, any ideas what’s wrong ? I thought YAML would be the recommended way  
> for  
> configfiles in ruby.
> 
> Or a simple alternative to yaml ?

YAML.load\_file returns the contents of the yaml file in a hash or an  
array  
(depending on the contents of the file, in your case is a hash). You  
need to  
store the result somewhere, like this:

config=YAML.load\_file('config.yaml)  
…  
config[‘cvsrepos’].each{ |x|  
…  
IO.popen("#{config[‘CVSEXE’]} -d  
…  
}

Trying to access CVSEXE, which ruby thinks is a constant, without having  
defined it, should rise a NameError exception, so I can’t understand why  
you  
don’t get an error message.

I hope this helps

Stefano

---

<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:** [March 13, 2007, 11:59am UTC](https://www.ruby-forum.com/t/yaml-load-file-problems/88531/4 "2007-03-13T11:59:50Z")

</div>

Hi Stefano,

---

<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:** [March 13, 2007, 9:16am UTC](https://www.ruby-forum.com/t/yaml-load-file-problems/88531/5 "2007-03-13T09:16:27Z")

</div>

Hi,
