Class: TclTkInterpreter

Inherits:
Object show all
Defined in:
lib/tcltk.rb

Overview

class TclTkInterpreter: tcl/tk interpreter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTclTkInterpreter

initialize():



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/tcltk.rb', line 88

def initialize()
  # generate interpreter object
  @ip = TclTkIp.new()

  # add ruby_fmt command to tcl interpreter
  # ruby_fmt command format arguments by `format' and call `ruby' command
  # (notice ruby command receives only one argument)
  if $DEBUG
    @ip._eval("proc ruby_fmt {fmt args} { puts \"ruby_fmt: $fmt $args\" ; set cmd [list ruby [format $fmt $args]] ; uplevel $cmd }")
  else
    @ip._eval("proc ruby_fmt {fmt args} { set cmd [list ruby [format $fmt $args]] ; uplevel $cmd }")
  end

  # @ip._get_eval_string(*args): generate string to evaluate in tcl interpreter
  #   *args: script which is going to be evaluated under tcl/tk
  def @ip._get_eval_string(*args)
    argstr = ""
    args.each{|arg|
      argstr += " " if argstr != ""
      # call to_eval if it is defined
      if (arg.respond_to?(:to_eval))
        argstr += arg.to_eval()
      else
        # call to_s unless defined
        argstr += arg.to_s()
      end
    }
    return argstr
  end

  # @ip._eval_args(*args): evaluate string under tcl/tk interpreter
  #     returns result string.
  #   *args: script which is going to be evaluated under tcl/tk
  def @ip._eval_args(*args)
    # calculate the string to eval in the interpreter
    argstr = _get_eval_string(*args)

    # evaluate under the interpreter
    print("_eval: \"", argstr, "\"") if $DEBUG
    res = _eval(argstr)
    if $DEBUG
      print(" -> \"", res, "\"\n")
    elsif  _return_value() != 0
      print(res, "\n")
    end
    fail(%Q/can't eval "#{argstr}"/) if _return_value() != 0 #'
    return res
  end

  # generate tcl/tk command object and register in the hash
  @commands = {}
  # for all commands registered in tcl/tk interpreter:
  @ip._eval("info command").split(/ /).each{|comname|
    if comname =~ /^[.]/
      # if command is a widget (path), generate TclTkWidget,
      # and register it in the hash
      @commands[comname] = TclTkWidget.new(@ip, comname)
    else
      # otherwise, generate TclTkCommand
      @commands[comname] = TclTkCommand.new(@ip, comname)
    end
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args) ⇒ Object

method_missing(id, *args): execute undefined method as tcl/tk command

id: method symbol
*args: method arguments


170
171
172
173
174
175
176
177
178
# File 'lib/tcltk.rb', line 170

def method_missing(id, *args)
  # if command named by id registered, then execute it
  if @commands.key?(id.id2name)
    return @commands[id.id2name].e(*args)
  else
    # otherwise, exception
    super
  end
end

Class Method Details

._eval_args(*args) ⇒ Object

@ip._eval_args(*args): evaluate string under tcl/tk interpreter

  returns result string.
*args: script which is going to be evaluated under tcl/tk


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/tcltk.rb', line 121

def @ip._eval_args(*args)
  # calculate the string to eval in the interpreter
  argstr = _get_eval_string(*args)

  # evaluate under the interpreter
  print("_eval: \"", argstr, "\"") if $DEBUG
  res = _eval(argstr)
  if $DEBUG
    print(" -> \"", res, "\"\n")
  elsif  _return_value() != 0
    print(res, "\n")
  end
  fail(%Q/can't eval "#{argstr}"/) if _return_value() != 0 #'
  return res
end

._get_eval_string(*args) ⇒ Object

@ip._get_eval_string(*args): generate string to evaluate in tcl interpreter

*args: script which is going to be evaluated under tcl/tk


103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/tcltk.rb', line 103

def @ip._get_eval_string(*args)
  argstr = ""
  args.each{|arg|
    argstr += " " if argstr != ""
    # call to_eval if it is defined
    if (arg.respond_to?(:to_eval))
      argstr += arg.to_eval()
    else
      # call to_s unless defined
      argstr += arg.to_s()
    end
  }
  return argstr
end

Instance Method Details

#_tcltkipObject

_tcltkip(): returns @ip(TclTkIp)



163
164
165
# File 'lib/tcltk.rb', line 163

def _tcltkip()
  return @ip
end

#commandsObject

commands(): returns hash of the tcl/tk commands



153
154
155
# File 'lib/tcltk.rb', line 153

def commands()
  return @commands
end

#rootwidgetObject

rootwidget(): returns root widget(TclTkWidget)



158
159
160
# File 'lib/tcltk.rb', line 158

def rootwidget()
  return @commands["."]
end