Class: VariableScopeStack

Inherits:
Object
  • Object
show all
Defined in:
lib/zombie_killer/variable_scope.rb

Overview

A stack of VariableScope

Instance Method Summary collapse

Constructor Details

#initializeVariableScopeStack

Returns a new instance of VariableScopeStack.



37
38
39
40
# File 'lib/zombie_killer/variable_scope.rb', line 37

def initialize
  outer_scope = VariableScope.new
  @stack = [outer_scope]
end

Instance Method Details

#innermostObject

The innermost, or current VariableScope



43
44
45
# File 'lib/zombie_killer/variable_scope.rb', line 43

def innermost
  @stack.last
end

#with_copy(&block) ⇒ Object

Run block using a copy of the innermost scope

Returns:

  • the scope as the block left it, popped from the stack



57
58
59
60
61
# File 'lib/zombie_killer/variable_scope.rb', line 57

def with_copy(&block)
  @stack.push innermost.dup
  block.call
  @stack.pop
end

#with_new(&block) ⇒ Object

Run block using a new clean scope

Returns:

  • the scope as the block left it, popped from the stack



49
50
51
52
53
# File 'lib/zombie_killer/variable_scope.rb', line 49

def with_new(&block)
  @stack.push VariableScope.new
  block.call
  @stack.pop
end