Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > expanding environment variable

Reply
Thread Tools

expanding environment variable

 
 
Daniel Schoch
Guest
Posts: n/a
 
      03-03-2009
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Daniel Schoch
Guest
Posts: n/a
 
      03-03-2009
Dominik Honnef wrote:
> On [Tue, 03.03.2009 23:28], Daniel Schoch 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"


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Daniel Schoch
Guest
Posts: n/a
 
      03-03-2009
Dominik Honnef 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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      03-03-2009
Dominik Honnef wrote:
> On [Tue, 03.03.2009 23:58], Daniel Schoch 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] }
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Daniel Schoch
Guest
Posts: n/a
 
      03-03-2009
Dominik Honnef wrote:
> On [Wed, 04.03.2009 00:16], Daniel Schoch 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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Gary Wright
Guest
Posts: n/a
 
      03-03-2009

On Mar 3, 2009, at 10:50 AM, Dominik Honnef 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 Wright

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-03-2009
On 03.03.2009 15:58, Daniel Schoch wrote:
> Dominik Honnef wrote:
>> On [Tue, 03.03.2009 23:28], Daniel Schoch 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"


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

 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      03-03-2009
[Note: parts of this message were removed to make it a legal post.]

On Tue, Mar 3, 2009 at 11:30 AM, Gary Wright <(E-Mail Removed)> wrote:

>
> On Mar 3, 2009, at 10:50 AM, Dominik Honnef 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.
>


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/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/pers...-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

 
Reply With Quote
 
Peña, Botp
Guest
Posts: n/a
 
      03-04-2009
RnJvbTogRGFuaWVsIFNjaG9jaCBbbWFpbHRvOnRyYXNoQHRla3 dpc3N1c2EuY29tXSANCiMgRG9t
aW5payBIb25uZWYgd3JvdGU6DQojID4gfCA+Pj4gRElSPXggRk lMRT15LnogaXJiDQojID4gfCA+
PiBzID0gIiRESVIvdGVzdC8kRklMRSIuZ3N1YigvXCRcdysvKS B7fG18IEVOVlttWzEuLi0xXV19
DQojID4gfCA9PiAieC90ZXN0L3kueiINCiMgUGVyZmVjdCwgdG hhbmtzLiBJIHNvbWV0aW1lcyBo
YXZlIGEgaGFyZCB0aW1lIHdpdGggdGhvc2UgDQojIGVmZmljaW VudCBvbmUtbGluZXJzLiBJdCdz
IGFsbCBzbyBjcnlwdGljLg0KDQoNCmlmIHdlIGFyZSBjYXJlZn VsIHcgY29uc3RhbnRzICh3YyB3
ZSBzaG91bGQpLCB3ZSBjYW4gZG8sDQoNCiAgcyA9ICJESVIvdG VzdC9GSUxFIi5nc3ViKC9bQS1a
XSsvKSB7fG18IEVOVlttXX0NCg0KKG9rLCBvaywgeW91IGd1ZX NzZWQgaXQsIGkgaGF0ZSBkb2xs
YXIgbm90YXRpb25zID0pDQoNCmtpbmQgcmVnYXJkcyAtYm90cA 0KDQoNCg0KDQoNCg0K

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
expanding a variable to a dict idle Python 8 04-09-2008 12:05 PM
Help on expanding variable from sh jdoe987@gmail.com Perl Misc 7 05-03-2007 05:57 PM
70-284 Lab Environment, Need Virtual Environment brooklynbridge508@hotmail.com MCSA 4 05-02-2007 09:49 AM
Setting an environment variable from another environment variable marcwentink@hotmail.com Java 5 04-04-2007 10:39 PM
[NEWBIE] Expanding a perl variable twice inside a substitution Tyson Marchuk Perl Misc 7 02-24-2004 05:47 PM



Advertisments