Run TCL in ruby

M

Michael Hale

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
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Run TCL in ruby"

|>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 :/

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
 
H

Hidetoshi NAGAI

From: Yukihiro Matsumoto <[email protected]>
Subject: Re: Run TCL in ruby
Date: Thu, 13 Jan 2005 08:25:39 +0900
Message-ID: said:
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
 
T

tony summerfelt

invokes Tcl interpreter directly without any inter-process
communication.
puts ip1._eval("button .lab -text exit -command \"destroy .\"").inspect

it's the interprocess communication i would like :) 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
 
H

Hidetoshi NAGAI

From: tony summerfelt <[email protected]>
Subject: Re: Run TCL in ruby
Date: Thu, 13 Jan 2005 12:22:34 +0900
Message-ID: said:
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,
------------------------------------------------------------------------
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
 
T

tony summerfelt

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 :)
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top