Class: SexpProcessor::Environment
Overview
I really hate this here, but I hate subdirs in my lib dir more… I guess it is kinda like shaving… I’ll split this out when it itches too much…
Instance Method Summary collapse
-
#[](name) ⇒ Object
Get
name
from env at whatever scope it is defined in, or return nil. -
#[]=(name, val) ⇒ Object
If
name
exists in the env, set it toval
in whatever scope it is in. -
#all ⇒ Object
Flatten out all scopes and return all key/value pairs.
-
#current ⇒ Object
Get the current/top environment.
-
#depth ⇒ Object
Return the current number of scopes.
-
#initialize ⇒ Environment
constructor
:nodoc:.
-
#scope ⇒ Object
Create a new scope and yield to the block passed.
Constructor Details
#initialize ⇒ Environment
:nodoc:
400 401 402 403 |
# File 'lib/sexp_processor.rb', line 400 def initialize #:nodoc: @env = [] @env.unshift({}) end |
Instance Method Details
#[](name) ⇒ Object
Get name
from env at whatever scope it is defined in, or return nil.
424 425 426 427 |
# File 'lib/sexp_processor.rb', line 424 def [] name hash = @env.find { |closure| closure.key? name } hash[name] if hash end |
#[]=(name, val) ⇒ Object
If name
exists in the env, set it to val
in whatever scope it is in. If it doesn’t exist, set name
to val
in the current scope.
434 435 436 437 |
# File 'lib/sexp_processor.rb', line 434 def []= name, val hash = @env.find { |closure| closure.key? name } || current hash[name] = val end |
#all ⇒ Object
Flatten out all scopes and return all key/value pairs.
408 409 410 |
# File 'lib/sexp_processor.rb', line 408 def all @env.reverse.inject { |env, scope| env.merge scope } end |
#current ⇒ Object
Get the current/top environment.
442 443 444 |
# File 'lib/sexp_processor.rb', line 442 def current @env.first end |
#depth ⇒ Object
Return the current number of scopes.
415 416 417 |
# File 'lib/sexp_processor.rb', line 415 def depth @env.length end |
#scope ⇒ Object
Create a new scope and yield to the block passed.
449 450 451 452 453 454 455 456 457 |
# File 'lib/sexp_processor.rb', line 449 def scope @env.unshift({}) begin yield ensure @env.shift raise "You went too far unextending env" if @env.empty? end end |