Skip to content

Instantly share code, notes, and snippets.

@iangreenleaf
Created November 19, 2012 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iangreenleaf/4112807 to your computer and use it in GitHub Desktop.
Save iangreenleaf/4112807 to your computer and use it in GitHub Desktop.
def with_var(variable, value, &block)
old_value = eval variable
eval "#{variable} = #{value.inspect}"
ret = block.call
eval "#{variable} = #{old_value.inspect}"
ret
end
# Usage
# =====
puts "Foo starts at #{ENV['FOO']}"
with_var "ENV['FOO']", "33" do
puts "Foo is now #{ENV['FOO']}"
end
puts "Foo ends at #{ENV['FOO']}"
@rubiety
Copy link

rubiety commented Nov 20, 2012

Be sure not to run with_var("FileUtils.rm_rf('/bad'); ENV['FOO']", 33) !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment