YAML.load_file problems

R

Rebhan, Gilbert

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 =3D


require "highline/import"

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

cvsrepos=3D%w[test test1 foo bar foobar]
cvsuser =3D ask("Enter CVS User: ") {|q|=20
q.default =3D "#{ENV["USERNAME"]}"
q.echo =3D true}
cvspass =3D ask("Enter your password: ") { |q| q.echo =3D '*' }

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


i tried with YAML, first step =3D

require 'yaml'=20
require "highline/import"

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

cvsrepos=3DYAML.load_file( "config.yaml" )

cvsuser =3D ask("Enter CVS User: ") {|q|=20
q.default =3D "#{ENV["USERNAME"]}"
q.echo =3D true}
cvspass =3D ask("Enter your password: ") { |q| q.echo =3D '*' }

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


config.yaml looks like =3D

---
- test
- test1
- foo
- bar
- foobar

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

require 'yaml'=20
require "highline/import"

YAML.load_file( "config.yaml" )

cvsuser =3D ask("Enter CVS User: ") {|q|=20
q.default =3D "#{ENV["USERNAME"]}"
q.echo =3D true}
cvspass =3D ask("Enter your password: ") { |q| q.echo =3D '*' }

cvsrepos.each {|x|
puts "Login CVS Repository >> #{x} ..."=20
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 =3D

---
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
 
S

Stefano Crocco

Alle luned=EC 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 ?

=46irst point: your second yaml file contains a mistake: you need to put a =
space=20
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=20
YAML.load_documents. It reads the data from a String or IO object, parses=20
each document and passes the result to the block. In your case, for example

=46ile.open("config.yaml") do |f|
YAML.load_documents(f) do |d|
p d
end
end

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

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

=2D--
CVSEXE: "//foobar/c$/tools/cvsnt/cvs.exe"
cvsrepos:
=2D test
=2D test1
=2D foo
=2D bar
=2D foobar

YAML.load_file('config.yaml')
=3D> "CVSEXE"=3D>"//foobar/c$/tools/cvsnt/cvs.exe", "cvsrepos"=3D>["test", =
"test1", "foo", "bar", "foobar"]}

I hope this helps

Stefano
 
R

Rebhan, Gilbert

=20
Hi,

-----Original Message-----
From: Stefano Crocco [mailto:[email protected]]=20
Sent: Monday, March 12, 2007 1:08 PM
To: ruby-talk ML
Subject: Re: YAML.load_file problems

/*
First point: your second yaml file contains a mistake: you need to put a
space=20
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 :)
*/

thanks for the pointer !

/*
At any rate, I don't think you need to use two yaml documents for what
you're=20
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')
=3D> "CVSEXE"=3D>"//foobar/c$/tools/cvsnt/cvs.exe", =
"cvsrepos"=3D>["test",
"test1", "foo", "bar", "foobar"]}
*/

yup that works in irb, but not in my script, i think the config.yaml
format is ok, but my access of
cvsrepos and CVSEXE is wrong =3D

require 'yaml'=20
require "highline/import"

YAML.load_file("config.yaml")

cvsuser =3D ask("Enter CVS User: ") {|q|=20
q.default =3D "#{ENV["USERNAME"]}"
q.echo =3D true}
cvspass =3D ask("Enter your password: ") { |q| q.echo =3D '*' }

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

and config.yaml

---
CVSEXE: "//foobar/c$/tools/cvsnt/cvs.exe"
cvsrepos:
- test
- test1
- foo
- bar
- foobar

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 ?

Regards, Gilbert
 
S

Stefano Crocco

Alle marted=EC 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=
=20
(depending on the contents of the file, in your case is a hash). You need t=
o=20
store the result somewhere, like this:

config=3DYAML.load_file('config.yaml)
=2E..
config['cvsrepos'].each{ |x|
=2E..
IO.popen("#{config['CVSEXE']} -d
=2E..
}

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

I hope this helps

Stefano
 
R

Rebhan, Gilbert

Hi Stefano,

-----Original Message-----
From: Stefano Crocco [mailto:[email protected]]=20
Sent: Tuesday, March 13, 2007 9:31 AM
To: ruby-talk ML
Subject: Re: YAML.load_file problems

/*

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

config=3DYAML.load_file('config.yaml)
...
config['cvsrepos'].each{ |x|
...
IO.popen("#{config['CVSEXE']} -d
...
}

*/

works fine :)
thanks

Regards, Gilbert
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top