I need to shell out to a process while setting an environment variable for it. I tried this one-liner:
system \"RBENV_VERSION=system ruby extconf.rb\"
Using your same approach, but wrapped up as a block method that temporarily modifies the environment (like the block form of Dir.chdir
):
def with_environment(variables={})
if block_given?
old_values = variables.map{ |k,v| [k,ENV[k]] }
begin
variables.each{ |k,v| ENV[k] = v }
result = yield
ensure
old_values.each{ |k,v| ENV[k] = v }
end
result
else
variables.each{ |k,v| ENV[k] = v }
end
end
with_environment 'RBENV_VERSION'=>'system' do
`ruby extconf.rb`
end