Hi,
How can I set environment variables to a program that I run using
Kernel#system?
Thanks,
Maurício
Hi,
How can I set environment variables to a program that I run using
Kernel#system?
Thanks,
Maurício
How can I set environment variables to a program that I run using
Kernel#system?
With ENV
pigeon% cat b.rb
#!/usr/bin/ruby
ENV['AA'] = "bb"
system('a.sh')
pigeon%
pigeon% cat a.sh
#!/bin/sh
echo $AA
pigeon%
pigeon% b.rb
bb
pigeon%
Guy Decoux
How can I set environment variables to a program that I run using
Kernel#system?
Hi Mauricio,
If you use Unix/Linux I think the normal ENV
should do the job.
Under Windows it’s more tricky.
I collected these snipplets that appeared
on that list some time ago.
def system(command)
Win32API.new(“crtdll”, “system”, [‘P’], ‘L’).Call(command)
end
def `(command)
popen = Win32API.new(“crtdll”, “_popen”, [‘P’,‘P’], ‘L’)
pclose = Win32API.new(“crtdll”, “_pclose”, [‘L’], ‘L’)
fread = Win32API.new(“crtdll”, “fread”, [‘P’,‘L’,‘L’,‘L’], ‘L’)
feof = Win32API.new(“crtdll”, “feof”, [‘L’], ‘L’)
saved_stdout = $stdout.clone
psBuffer = " " * 128
rBuffer = “”
f = popen.Call(command,“r”)
while feof.Call( f )==0
l = fread.Call( psBuffer,1,128,f )
rBuffer += psBuffer[0…l]
end
pclose.Call f
$stdout.reopen(saved_stdout)
rBuffer
end
I tested on the version of ruby I have in windows (1.72) and the ENV
way worked. However, the code you show seems pretty interesting!
Thanks,
Maurício
Armin Roehrl wrote:
(…)
Under Windows it’s more tricky.I collected these snipplets that appeared
on that list some time ago.
(…)