Previous Reductive Labs, LLC Next

Blocks: Any Cleanup

E.g., environment cleanup:

def withenv(hash)
    oldvals = {}
    hash.each do |name, val|
        name = name.to_s
        oldvals[name] = ENV[name]
        ENV[name] = val
    end

    yield
ensure
    oldvals.each do |name, val|
        ENV[name] = val
    end
end

ENV["testing"] = "initial"

withenv :testing => "interior" do
    puts "Inside the method:  " + ENV["testing"]
end

puts "Outside the method:  " + ENV["testing"]

Produces:

Inside the method:  interior
Outside the method:  initial
Previous Luke Kanies luke@reductivelabs.com | Index Next