Question on how to source a file with variables in it.

O

Oscar Gon

I'm just trying to learn some scripting in Ruby and I am trying to
convert a shell script to ruby.

part of the shell script does this:

source ./filename.txt


"filename.txt" contains variables, so then once its done sourcing it,
those variables are available for the rest of the script, and a number
of other scripts that use the same values.

I can't figure out how to do something like that in Ruby.

I already have a file with the values I need in it, it sits in the same
directory level as the script I'm running. Can someone point me to the
right calls or objects, how I can source it in a similar or better way
than shell/bash?

The syntax in the file I created is just like I've been defining
variables in ruby...

url = http://www.anydomain.com
contact = Joe
contactlast = Smith



Or if you have another suggestion with a more elegant solution, that
would be appreciated as well. I currenly have to copy and paste all the
variables section to each of the scripts that uses them when I update
them.

thanks!
 
E

Ezra Zygmuntowicz



The only way to do it with plain old local variables that I know of
is to use eval.


def load_vars(filname)
eval(File.open(filename).read)
end

-Ezra
 
H

hemant kumar

Or convert you file to a YAML.


-----Original Message-----
From: Ezra Zygmuntowicz [mailto:[email protected]]
Sent: Wednesday, August 09, 2006 6:52 AM
To: ruby-talk ML
Subject: Re: Question on how to source a file with variables in it.




The only way to do it with plain old local variables that I know of is to
use eval.


def load_vars(filname)
eval(File.open(filename).read)
end

-Ezra
 
O

Oscar Gon

Ezra said:
The only way to do it with plain old local variables that I know of
is to use eval.


def load_vars(filname)
eval(File.open(filename).read)
end

-Ezra

Thanks Ezra, that's exactly what I was looking for. I'll give it a try!

@ hemant kumar

Thanks, I took a look at the yaml page but I'm not sure how I would use
that in ruby. I understand the syntax that's used in YAML (I think) but
I'd still be left wondering how to read that into all/any of the ruby
scripts I need.
 
M

Mike Fletcher

Oscar said:
Thanks, I took a look at the yaml page but I'm not sure how I would use
that in ruby. I understand the syntax that's used in YAML (I think) but
I'd still be left wondering how to read that into all/any of the ruby
scripts I need.

vars.yml

url: http://www.example.com/
contact:
first: Joe
last: Smith


Then

require 'yaml'

data = YAML::load_file( "vars.yml" )

puts "#{ data['contact']['first'] } from #{ data['url'] }"
 

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,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top