Class: TkVarAccess (Ruby 2.3.4)

In Files

  • tk/lib/tk/variable.rb

Parent

Class/Module Index [+]

Quicksearch

TkVarAccess

Public Class Methods

new(name, *args)
 
               # File tk/lib/tk/variable.rb, line 1653
def self.new(name, *args)
  if name.kind_of?(TkVariable)
    name.value = args[0] unless args.empty?
    return name
  end

  name = name.to_s
  v = nil
  TkVar_ID_TBL.mutex.synchronize{
    if v = TkVar_ID_TBL[name]
      v.value = args[0] unless args.empty?
      return v
    else
      (v = self.allocate).instance_eval{
        @id = name
        TkVar_ID_TBL[@id] = self
        @var = @id
      }
    end
  }

  v.instance_eval{ initialize(name, *args) }
  v
end
            
new(varname, val=nil)
 
               # File tk/lib/tk/variable.rb, line 1714
  def initialize(varname, val=nil)
    # @id = varname
    # TkVar_ID_TBL[@id] = self

    # @var  = @id
    @elem = nil

    @def_default = false
    @default_val = nil

    @trace_var  = nil
    @trace_elem = nil
    @trace_opts = nil

    @type = nil
    var = self
    @element_type = Hash.new{|k,v| var.default_value_type }

    # is an element?
    if @id =~ /^([^(]+)\((.+)\)$/
      # is an element --> var == $1, elem == $2
      @var  = $1
      @elem = $2
    end

    # teach Tk-ip that @id is global var
    INTERP._invoke_without_enc('global', @var)
    begin
      INTERP._invoke_without_enc('global', @id)
    rescue => e
      if @id =~ /^(.+)\([^()]+\)$/
        # is an element --> varname == $1
        INTERP._invoke_without_enc('global', $1)
      else
        fail e
      end
    end

    if val
      if val.kind_of?(Hash)
        # assoc-array variable
        self[''] = 0
        self.clear
      end
      #s = '"' + _get_eval_string(val).gsub(/[\[\]$"]/, '\\\\\&') + '"' #"
      #s = '"' + _get_eval_string(val).gsub(/[\[\]$"\\]/, '\\\\\&') + '"' #"
      #INTERP._eval(Kernel.format('global %s; set %s %s', @id, @id, s))
      #INTERP._set_global_var(@id, _toUTF8(_get_eval_string(val)))
      self.value = val
    end
  end
            
new_hash(name, *args)
 
               # File tk/lib/tk/variable.rb, line 1678
def self.new_hash(name, *args)
  if name.kind_of?(TkVariable)
    unless name.is_hash?
      fail ArgumentError, "already exist as a scalar variable"
    end
    name.value = args[0] unless args.empty?
    return name
  end

  name = name.to_s
  v = nil
  TkVar_ID_TBL.mutex.synchronize{
    if v = TkVar_ID_TBL[name]
      unless v.is_hash?
        fail ArgumentError, "already exist as a scalar variable"
      end
      v.value = args[0] unless args.empty?
      return v
    else
      (v = self.allocate).instance_eval{
        @id = name
        TkVar_ID_TBL[@id] = self
        @var = @id
      }
    end
  }

  INTERP._invoke_without_enc('global', name)
  if args.empty? && INTERP._invoke_without_enc('array', 'exist', name) == '0'
    v.instance_eval{ initialize(name, {}) }  # force creating
  else
    v.instance_eval{ initialize(name, *args) }
  end
  v
end