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

**URL:** <https://www.ruby-forum.com/t/question-on-how-to-source-a-file-with-variables-in-it/101984>\
**Category:** Ruby\
**Created:** [August 9, 2006, 2:44am UTC](https://www.ruby-forum.com/t/question-on-how-to-source-a-file-with-variables-in-it/101984 "2006-08-09T02:44:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![muychingon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8491ac/32.png) [@muychingon](https://www.ruby-forum.com/u/muychingon)\
**Post date:** [August 9, 2006, 2:44am UTC](https://www.ruby-forum.com/t/question-on-how-to-source-a-file-with-variables-in-it/101984/1 "2006-08-09T02:44:33Z")

</div>

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](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!

---

<div class="post-metadata">

**Author:** ![muychingon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8491ac/32.png) [@muychingon](https://www.ruby-forum.com/u/muychingon)\
**Post date:** [August 9, 2006, 2:46am UTC](https://www.ruby-forum.com/t/question-on-how-to-source-a-file-with-variables-in-it/101984/2 "2006-08-09T02:46:57Z")

</div>

I meant:

> url = ‘[http://www.anydomain.com](http://www.anydomain.com)’  
> contact = ‘Joe’  
> contactlast = ‘Smith’

---

<div class="post-metadata">

**Author:** ![muychingon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8491ac/32.png) [@muychingon](https://www.ruby-forum.com/u/muychingon)\
**Post date:** [August 9, 2006, 3:24am UTC](https://www.ruby-forum.com/t/question-on-how-to-source-a-file-with-variables-in-it/101984/3 "2006-08-09T03:24:50Z")

</div>

On Aug 8, 2006, at 5:46 PM, Oscar Gon wrote:

> I meant:
> 
> > url = ‘[http://www.anydomain.com](http://www.anydomain.com)’  
> > contact = ‘Joe’  
> > contactlast = ‘Smith’
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

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

---

<div class="post-metadata">

**Author:** ![muychingon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8491ac/32.png) [@muychingon](https://www.ruby-forum.com/u/muychingon)\
**Post date:** [August 9, 2006, 4:43pm UTC](https://www.ruby-forum.com/t/question-on-how-to-source-a-file-with-variables-in-it/101984/4 "2006-08-09T16:43:19Z")

</div>

Ezra Z. wrote:

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

---

<div class="post-metadata">

**Author:** ![muychingon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8491ac/32.png) [@muychingon](https://www.ruby-forum.com/u/muychingon)\
**Post date:** [August 9, 2006, 5:31am UTC](https://www.ruby-forum.com/t/question-on-how-to-source-a-file-with-variables-in-it/101984/5 "2006-08-09T05:31:02Z")

</div>

Or convert you file to a YAML.

---

<div class="post-metadata">

**Author:** ![muychingon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8491ac/32.png) [@muychingon](https://www.ruby-forum.com/u/muychingon)\
**Post date:** [August 9, 2006, 4:52pm UTC](https://www.ruby-forum.com/t/question-on-how-to-source-a-file-with-variables-in-it/101984/6 "2006-08-09T16:52:31Z")

</div>

Oscar Gon wrote:

> 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/](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’] }”
