Created
November 19, 2012 18:49
-
-
Save iangreenleaf/4112807 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be sure not to run
with_var("FileUtils.rm_rf('/bad'); ENV['FOO']", 33)
!