# Expanding environment variable

**URL:** <https://www.ruby-forum.com/t/expanding-environment-variable/161899>\
**Category:** Ruby\
**Created:** [March 3, 2009, 3:29pm UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899 "2009-03-03T15:29:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [March 3, 2009, 3:29pm UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899/1 "2009-03-03T15:29:44Z")

</div>

Hi,

I have a string  
s = “$DIR/test/$FILE”

Is there an easy way to expand this with the defined environment  
variables?  
I can always write a quick parser, but I was wondering maybe this  
already exists?

Thanks  
ds

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [March 3, 2009, 3:54pm UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899/2 "2009-03-03T15:54:10Z")

</div>

On [Tue, 03.03.2009 23:28], Daniel S. wrote:

> Hi,
> 
> I have a string  
> s = “$DIR/test/$FILE”
> 
> Is there an easy way to expand this with the defined environment  
> variables?  
> What do you mean by environment variables? The stuff in ENV, or the  
> constants like **FILE** or simply global variables?

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [March 3, 2009, 3:59pm UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899/3 "2009-03-03T15:59:50Z")

</div>

Dominik H. wrote:

> On [Tue, 03.03.2009 23:28], Daniel S. wrote:
> 
> > Hi,
> > 
> > I have a string  
> > s = “$DIR/test/$FILE”
> > 
> > Is there an easy way to expand this with the defined environment  
> > variables?  
> > What do you mean by environment variables? The stuff in ENV, or the  
> > constants like **FILE** or simply global variables?

I mean the stuff in ENV.  
SHELL:  
export DIR=x  
export FILE=y.z

RUBY:  
I’d like to know the easiest way from  
s = “$DIR/test/$FILE”  
to  
s = “x/test/y.z”

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [March 3, 2009, 4:11pm UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899/4 "2009-03-03T16:11:15Z")

</div>

On [Tue, 03.03.2009 23:58], Daniel S. wrote:

> > constants like **FILE** or simply global variables?  
> > s = “x/test/y.z”

| \>\>\> DIR=x FILE=y.z irb  
| \>\> s = “$DIR/test/$FILE”.gsub(/$\w+/) {|m| ENV[m[1…-1]]}  
| =\> “x/test/y.z”

Thats how I would do it.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [March 3, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899/5 "2009-03-03T16:17:18Z")

</div>

Dominik H. wrote:

> | \>\>\> DIR=x FILE=y.z irb  
> | \>\> s = “$DIR/test/$FILE”.gsub(/$\w+/) {|m| ENV[m[1…-1]]}  
> | =\> “x/test/y.z”
> 
> Thats how I would do it.

Perfect, thanks. I sometimes have a hard time with those efficient  
one-liners. It’s all so cryptic.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [March 3, 2009, 4:48pm UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899/6 "2009-03-03T16:48:04Z")

</div>

Dominik H. wrote:

> On [Tue, 03.03.2009 23:58], Daniel S. wrote:
> 
> > > constants like **FILE** or simply global variables?  
> > > s = “x/test/y.z”
> 
> | \>\>\> DIR=x FILE=y.z irb  
> | \>\> s = “$DIR/test/$FILE”.gsub(/$\w+/) {|m| ENV[m[1…-1]]}  
> | =\> “x/test/y.z”
> 
> Thats how I would do it.

Or: s = “$DIR/test/$FILE”.gsub(/$(\w+)/) { ENV[$1] }

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [March 3, 2009, 4:51pm UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899/7 "2009-03-03T16:51:19Z")

</div>

On [Wed, 04.03.2009 00:16], Daniel S. wrote:

> Perfect, thanks. I sometimes have a hard time with those efficient  
> one-liners. It’s all so cryptic.

Well, the only really cryptic thing about this one is the regexp, but  
thats just a matter of learning regular expressions. And well, the  
rest are basic ruby idioms/features like blocks. Comes time comes  
knowledge, you will get used to those one-liners 🙂

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [March 3, 2009, 4:52pm UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899/8 "2009-03-03T16:52:30Z")

</div>

On [Wed, 04.03.2009 00:46], Brian C. wrote:

> > Thats how I would do it.
> 
> Or: s = “$DIR/test/$FILE”.gsub(/$(\w+)/) { ENV[$1] }  
> Probably depends on whether you like global variables or not. It is  
> definitely shorter/more readable and I actually didn’t know if gsub  
> sets $1.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [March 3, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899/9 "2009-03-03T17:26:01Z")

</div>

Dominik H. wrote:

> On [Wed, 04.03.2009 00:16], Daniel S. wrote:
> 
> > Perfect, thanks. I sometimes have a hard time with those efficient  
> > one-liners. It’s all so cryptic.
> 
> Well, the only really cryptic thing about this one is the regexp, but  
> thats just a matter of learning regular expressions. And well, the  
> rest are basic ruby idioms/features like blocks. Comes time comes  
> knowledge, you will get used to those one-liners 🙂

That is true. I’m used to the old-fashioned programming of C, assembly  
and such. There, things go with baby-steps, you do one little thing at a  
time.  
All of a sudden with perl/ruby and such you can do tons of stuff with  
few lines. My brain is not wired for this. I have a hard time forming a  
ruby-worthy solution around a problem, as I’m always falling back to C’s  
small step way of thinking. But you’re right, comes time comes  
knowledge.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [March 3, 2009, 5:33pm UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899/10 "2009-03-03T17:33:43Z")

</div>

On Mar 3, 2009, at 10:50 AM, Dominik H. wrote:

> > Or: s = “$DIR/test/$FILE”.gsub(/$(\w+)/) { ENV[$1] }  
> > Probably depends on whether you like global variables or not. It is  
> > definitely shorter/more readable and I actually didn’t know if gsub  
> > sets $1.

$1 isn’t a true global variable. It is a per-thread variable. It  
still is a bit ‘magic’ for many people’s tastes but it isn’t as bad  
as a true global.

Gary W.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [March 3, 2009, 6:40pm UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899/11 "2009-03-03T18:40:58Z")

</div>

On 03.03.2009 15:58, Daniel S. wrote:

> > constants like **FILE** or simply global variables?  
> > s = “x/test/y.z”  
> > Just wondering: is there anything that would prevent doing this:

s = “#{ENV[“DIR”]/test/#{ENV[“FILE”]}”

or even

s = File.join ENV[“DIR”], “test”, ENV[“FILE”]

? In other words: is the format of your original string mandatory? If  
not, I’d rather choose one of the other approaches.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [March 3, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899/12 "2009-03-03T19:06:20Z")

</div>

On Tue, Mar 3, 2009 at 11:30 AM, Gary W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> $1 isn’t a true global variable. It is a per-thread variable. It  
> still is a bit ‘magic’ for many people’s tastes but it isn’t as bad  
> as a true global.

Actually its really a frame local variable. heres a bit of artificial  
code  
which illustrates this:

def m1(string)  
string.scan(/(ab|ac)/) {|m|  
puts “in m1 $1 is #{$1}”  
result = m2($1)  
puts “result is #{result}, $1 is #{$1}”  
}  
end

def m2(string)  
“z#{string}”.scan(/(za)/) {|m|  
puts “in m2 $1 is #{$1}”  
$1  
}  
end

m1(“abcac”) # =\> “abcac”

# \>\> in m1 $1 is ab

# \>\> in m2 $1 is za

# \>\> result is zab, $1 is ab

# \>\> in m1 $1 is ac

# \>\> in m2 $1 is za

# \>\> result is zac, $1 is ac

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [March 4, 2009, 3:14am UTC](https://www.ruby-forum.com/t/expanding-environment-variable/161899/13 "2009-03-04T03:14:52Z")

</div>

From: Daniel S. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Dominik H. wrote:

# \> | \>\>\> DIR=x FILE=y.z irb

# \> | \>\> s = “$DIR/test/$FILE”.gsub(/$\w+/) {|m| ENV[m[1…-1]]}

# \> | =\> “x/test/y.z”

# Perfect, thanks. I sometimes have a hard time with those

# efficient one-liners. It’s all so cryptic.

if we are careful w constants (wc we should), we can do,

s = “DIR/test/FILE”.gsub(/[A-Z]+/) {|m| ENV[m]}

(ok, ok, you guessed it, i hate dollar notations =)

kind regards -botp
