Class: Tk::Variable

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/ffi-tk/variable.rb

Overview

This class is used for communication of variables with Tcl.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value = None) ⇒ Variable

Returns a new instance of Variable.



9
10
11
12
13
# File 'lib/ffi-tk/variable.rb', line 9

def initialize(name, value = None)
  @name = name.freeze
  @tcl_name = "$#{name}"
  set(value) unless None == value
end

Instance Attribute Details

#bytesizeObject (readonly)

Returns the value of attribute bytesize.



7
8
9
# File 'lib/ffi-tk/variable.rb', line 7

def bytesize
  @bytesize
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/ffi-tk/variable.rb', line 7

def name
  @name
end

#tcl_nameObject (readonly)

Returns the value of attribute tcl_name.



7
8
9
# File 'lib/ffi-tk/variable.rb', line 7

def tcl_name
  @tcl_name
end

Instance Method Details

#<=>(other) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/ffi-tk/variable.rb', line 59

def <=>(other)
  case other
  when self.class
    get <=> other.get
  else
    get <=> other
  end
end

#getObject



15
16
17
18
19
# File 'lib/ffi-tk/variable.rb', line 15

def get
  Tk.execute('set', name)
rescue RuntimeError
  raise NameError, "can't read %p: no such variable" % [name]
end

#set(value) ⇒ Object



21
22
23
# File 'lib/ffi-tk/variable.rb', line 21

def set(value)
  Tk.execute_only('set', name, value)
end

#to_booleanObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ffi-tk/variable.rb', line 41

def to_boolean
  got = get

  if got.respond_to?(:to_boolean)
    got.to_boolean
  elsif got == '0'
    false
  elsif got == '1'
    true
  else
    got
  end
end

#to_fObject



55
56
57
# File 'lib/ffi-tk/variable.rb', line 55

def to_f
  get.to_f
end

#to_iObject



37
38
39
# File 'lib/ffi-tk/variable.rb', line 37

def to_i
  get.to_i
end

#to_sObject



33
34
35
# File 'lib/ffi-tk/variable.rb', line 33

def to_s
  get.to_s
end

#to_tclObject



29
30
31
# File 'lib/ffi-tk/variable.rb', line 29

def to_tcl
  TclString.new(name)
end

#unsetObject



25
26
27
# File 'lib/ffi-tk/variable.rb', line 25

def unset
  Tk.execute_only('unset', name)
end