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.



39
40
41
42
# File 'lib/zombie_killer/variable_scope.rb', line 39

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

Instance Method Details

#innermostObject

The innermost, or current VariableScope



45
46
47
# File 'lib/zombie_killer/variable_scope.rb', line 45

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



59
60
61
62
63
# File 'lib/zombie_killer/variable_scope.rb', line 59

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



51
52
53
54
55
# File 'lib/zombie_killer/variable_scope.rb', line 51

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