Run TCL in ruby

Is there a simple way to run TCL code from ruby? Basically I have a shared TCL library and I want to call the functions it has from Ruby not from TCL.

Thanks, Michael

from the examples posted here it's relatively simple, but
communicating with between ruby and tcl seems to be a
nightmare :confused:

http://home.cogeco.ca/~tsummerfelt1
telnet://ventedspleen.dyndns.org

···

On Thu, 13 Jan 2005 00:06:47 +0900, you wrote:

Is there a simple way to run TCL code from ruby?

Hi,

Is there a simple way to run TCL code from ruby?

from the examples posted here it's relatively simple, but
communicating with between ruby and tcl seems to be a
nightmare :confused:

The tcltklib extension, which comes with the standard distribution,
invokes Tcl interpreter directly without any inter-process
communication.

              matz.
  require "tcltklib"

  def test
    ip1 = TclTkIp.new()

    puts ip1._eval("button .lab -text exit -command \"destroy .\"").inspect
    puts ip1._eval("pack .lab").inspect

    puts ip1._eval(%q+puts [ruby {print "print by ruby\n"; "puts by tcl/tk"}]+).inspect
    TclTkLib.mainloop
  end

  test

···

In message "Re: Run TCL in ruby" on Thu, 13 Jan 2005 02:11:55 +0900, tony summerfelt <snowzone5@hotmail.com> writes:

Message-ID: <1105572352.822991.7804.nullmailer@x31.priv.netlab.jp>

    ip1 = TclTkIp.new()

If no need for Tk, an IP without Tk can be created.
To do that, please call TclTkIp.new(nil, false).
                                         ^^^^^
TclTkIP#_split_tklist(str), _merge_tklist(str, ...) and
_conv_listelement(str) may be useful (see MANUAL.eng of tcltklib).

If use Ruby/Tk, module methods of TkComm may be useful too.

* NOTE: This method list is based on Ruby-1.8.2.

   tk_tcl2ruby(val)
        Tcl string -> Ruby object
        # If seems to a number, then returns Numeric.
        # Or If seems to a widget path, returns a widget object.
        # So, sometimes wrong estimated object.

   tk_split_list(val)
        Tcl list -> Ruby array (each element converted by tk_tcl2ruby())
        # do recursive call, if sub-string seems to a list.

   tk_split_simplelist(val)
        Tcl list -> Ruby array of string
        # do NOT recursive call.

   array2tk_list(ary)
        Ruby array -> Tcl list

   _symbolkey2str(hash)
        return new Hash of which Symbol keys are changed to String keys.
        # {:key=>val, ... } -> {'key'=>val, ... }

   hash_kv(hash)
        {key=>val, key=>val, ...} -> ['-key', val, '-key', val, ...]
        # to treat option arguments

   bool(str)
        Tcl string -> Ruby boolean (true/false)

   number(str)
        Tcl string -> Ruby numeric

   string(str)
        Tcl string -> Ruby string
        # If str is wrapped by '{' and '}', then remove them.
        # Else, no operation.

   num_or_str(str)
        Tcl string -> Ruby numeric or string
        # If str seems to a number, then returns number(str).
        # Else, returns string(str).

   list(str)
        same to tk_split_list(str)

   simplelist(str)
        same to tk_split_simplelist(str)

   window(str)
        Tcl widget path -> Ruby widget object

   procedure(str)
        Tcl callback str -> Ruby procedure object
        # If str is entried as a callback (e.g. 'rb_out ...';
        # see install_cmd()), returns a procedure.
        # Else, returns str.

   install_cmd(cmd)
        Entries cmd as a callback and returns a string which should be
        passed to Tcl interpreter.

   _get_eval_string(obj)
        Ruby obj -> Tcl string

···

From: Yukihiro Matsumoto <matz@ruby-lang.org>
Subject: Re: Run TCL in ruby
Date: Thu, 13 Jan 2005 08:25:39 +0900

--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

it's the interprocess communication i would like :slight_smile: in your example
above i would like the -command to execute ruby method/code

although i really like the syntax:

Tk.tk_call('source', 'gui.tcl')

gui.tcl contains the entire interface for the program, but i would
like the -command associated with each button to execute ruby code,
not tcl code...i also need to get all the text from a tcl text
widget...

http://home.cogeco.ca/~tsummerfelt1
telnet://ventedspleen.dyndns.org

···

On Thu, 13 Jan 2005 08:25:39 +0900, you wrote:

invokes Tcl interpreter directly without any inter-process
communication.

   puts ip1._eval("button .lab -text exit -command \"destroy .\"").inspect

Message-ID: <o2qbu055a4prok0snie1gbt7qkhufvl71f@4ax.com>

although i really like the syntax:

Tk.tk_call('source', 'gui.tcl')

gui.tcl contains the entire interface for the program, but i would
like the -command associated with each button to execute ruby code,
not tcl code...i also need to get all the text from a tcl text
widget...

If you know the widget path on the tcl script and don't need to use
the Tcl code associated -command option, those are very easy.

When the widget path of the button is .foo.bar.btn and the path of
the text is .baz.txt, for example,

···

From: tony summerfelt <snowzone5@hotmail.com>
Subject: Re: Run TCL in ruby
Date: Thu, 13 Jan 2005 12:22:34 +0900
------------------------------------------------------------------------
Tk.tk_call('source', 'gui.tcl')

def show_text(txt)
   p txt.value
end

txt = TkText.new(:widgetname=>'.baz.txt', :without_creating=>true)

b = TkButton.new(:widgetname=>'.foo.bar.btn', :without_creating=>true)
b.command(proc{ show_text(txt) }) # or b.command{ show_text(txt) }

# or
# TkButton.new(:widgetname=>'.foo.bar.btn', :without_creating=>true){
# command { show_text(txt) }
# }

Tk.mainloop
------------------------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

If you know the widget path on the tcl script and don't need to use
the Tcl code associated -command option, those are very easy.

ok :slight_smile:

txt = TkText.new(:widgetname=>'.baz.txt', :without_creating=>true)

i got this far from the code you posted previously

def show_text(txt)
  p txt.value
end

b = TkButton.new(:widgetname=>'.foo.bar.btn', :without_creating=>true)
b.command(proc{ show_text(txt) }) # or b.command{ show_text(txt) }

Tk.mainloop

this is pretty much exactly what i wanted. much simpler than the code
you posted earlier...

http://home.cogeco.ca/~tsummerfelt1
telnet://ventedspleen.dyndns.org

···

On Thu, 13 Jan 2005 13:07:44 +0900, you wrote: