From: Ara.T.Howard@... Date: 2004-10-14T01:44:33+09:00 Subject: Re: [nuby] shell-like substitution in a string On Thu, 14 Oct 2004, Lionel Thiry wrote: > Hi! > > I'd like to make some kind of substitution like in a shell, i.e. having: > "${tool} -o ${target} -L${libpath} -l${lib} ${source}" > > with an env like this: > env = { > "tool" => "gcc", > "target" => "hello.exe", > "libpath" => "/usr/lib", > "lib" => "somelib", > "source" => "hello.c" > } > > it should give: > "gcc -o hello.exe -L/usr/lib -lsomelib hello.c" > > > It is easy until I want to manage caracter escapement like > "${var\\}weird}" which should give env["var}weird"], "\\${var}" which > should give the simple string "${var}" and "\\\\${var}" which should be > equivalent to "\\" + env["var"]. i don't think you need to worry about that since 'var}weird' is not a valid shell identifier - at least i can't seem to create a var by that name in the shell... > Any idea of how I could manage that? you can get bash's exact parameter expansion like this: jib:~ > cat a.rb require 'session' class Expander def self::new(*a, &b) @instance = super end def initialize @bash = Session::Bash::new end def expand expr, context = {} context.each do |k,v| o, e = @bash.execute "#{ k }=#{ v }" raise e unless e.empty? end stdout, stderr = @bash.execute "cat < "gcc", "target" => "hello.exe", "libpath" => "/usr/lib", "lib" => "somelib", "source" => "hello.c" } cmd = ex.expand expr, env p cmd jib:~ > ruby a.rb "gcc -o hello.exe -L/usr/lib -lsomelib hello.c" -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================