Module: Kernel
- Defined in:
- lib/core_extensions/kernel.rb
Instance Method Summary collapse
-
#with_locals(locals = {}, &block) ⇒ Object
yields an alternate reality block where instance methods are different from what they were.
Instance Method Details
#with_locals(locals = {}, &block) ⇒ Object
yields an alternate reality block where instance methods are different from what they were. At the end of the block it resets the initial values of the instance variables.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/core_extensions/kernel.rb', line 5 def with_locals locals = {}, &block old_local_vars = {} for k, v in locals var = :"@#{k}" old_local_vars[k] = instance_variable_get(var) instance_variable_set( var, v ) end yield ensure # make all as it once was for k, v in old_local_vars var = :"@#{k}" instance_variable_set( var, v ) end end |