Managing Ruby Environment Variables
Author: Samuel Williams When: Tuesday, 21 April 2009April 2009
May 2009
August 2009
September 2009
October 2009
- Building a Concrete Bath
- LED Lighting Comparison
- Thinking about Programming Languages
- How To Be A Consultant
- Lucid Programming Dojo
- Exim4 + ClamAV + SpamAssassin
- Secure login using AJAX
- Ramaze And Rack
- ActiveMerchant
- Concurrency And Immutability
- Floating Point Numbers
- Programming And Debugging
- Useful jQuery Plugins
- Loading Anonymous Ruby Classes
- 尺八 (Shakuhachi)
- Card Trick
- Object Oriented C
- Gemcutter
- Writing Clearly
- Richard Stallman In Christchurch
- Magnatune
- Client Side Graphing
- Zena CMS
November 2009
February 2010
March 2010
April 2010
May 2010
June 2010
July 2010
August 2010
September 2010
December 2010
January 2011
March 2011
May 2011
August 2011
September 2011
I was writing a Rakefile that required to modify the ENV (environment) for scripts being executed using system(). Because I couldn't figure out how to pass environment variables directly to the child process, I've made a simple function which can (in a non-thread-safe manor) assist with this minor problem:
def environment (new_env = nil, &block) old_env = ENV.to_hash ENV.update(new_env) if new_env yield ENV.clear ENV.update(old_env) end
It is typically used as follows:
build_env = {"CC" => "/usr/bin/gcc"}
environment(build_env) do
system("make")
end
Comments
Please note, you can leave a comment that uses (limited) XHTML and Textile syntax.