Class: RubyParserStuff::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_parser/ruby_parser_extras.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dyn = false) ⇒ Environment

Returns a new instance of Environment.



958
959
960
961
962
963
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 958

def initialize dyn = false
  @dyn = []
  @env = []
  @use = []
  self.reset
end

Instance Attribute Details

#dynObject (readonly)

Returns the value of attribute dyn.



923
924
925
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 923

def dyn
  @dyn
end

#envObject (readonly)

Returns the value of attribute env.



923
924
925
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 923

def env
  @env
end

Instance Method Details

#[](k) ⇒ Object



925
926
927
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 925

def [] k
  self.all[k]
end

#[]=(k, v) ⇒ Object



929
930
931
932
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 929

def []= k, v
  raise "no" if v == true
  self.current[k] = v
end

#allObject



934
935
936
937
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 934

def all
  idx = @dyn.index(false) || 0
  @env[0..idx].reverse.inject { |env, scope| env.merge scope }
end

#currentObject



939
940
941
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 939

def current
  @env.first
end

#dynamicObject



943
944
945
946
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 943

def dynamic
  idx = @dyn.index false
  @env[0...idx].reverse.inject { |env, scope| env.merge scope } || {}
end

#dynamic?Boolean

Returns:

  • (Boolean)


948
949
950
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 948

def dynamic?
  @dyn[0] != false
end

#extend(dyn = false) ⇒ Object



952
953
954
955
956
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 952

def extend dyn = false
  @dyn.unshift dyn
  @env.unshift({})
  @use.unshift({})
end

#resetObject



965
966
967
968
969
970
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 965

def reset
  @dyn.clear
  @env.clear
  @use.clear
  self.extend
end

#unextendObject



972
973
974
975
976
977
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 972

def unextend
  @dyn.shift
  @env.shift
  @use.shift
  raise "You went too far unextending env" if @env.empty?
end

#use(id) ⇒ Object



979
980
981
982
983
984
985
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 979

def use id
  @env.each_with_index do |env, i|
    if env[id] then
      @use[i][id] = true
    end
  end
end

#used?(id) ⇒ Boolean

Returns:

  • (Boolean)


987
988
989
990
991
# File 'lib/ruby_parser/ruby_parser_extras.rb', line 987

def used? id
  idx = @dyn.index false # REFACTOR
  u = @use[0...idx].reverse.inject { |env, scope| env.merge scope } || {}
  u[id]
end