From: Rick DeNatale Date: 2009-03-04T03:04:23+09:00 Subject: Re: expanding environment variable --0016361e88064cce3804643ac85e Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Tue, Mar 3, 2009 at 11:30 AM, Gary Wright 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/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale --0016361e88064cce3804643ac85e--