From: Hidetoshi NAGAI Date: 2012-07-12T03:47:59+09:00 Subject: Re: "Register" Ruby function as Tcl function in Ruby-Tk? From: Kevin Walzer Subject: "Register" Ruby function as Tcl function in Ruby-Tk? Date: Sat, 30 Jun 2012 22:19:54 +0900 Message-ID: <4FEEFCF6.20501@codebykevin.com> > Python has a "register" function that wraps a Python function into a > Tcl function and returns the new Tcl function's name. This is > sometimes used in Python-Tk applications for functions that need to be > attached to Tk widgets in some complex way. Does Ruby-Tk have any > similar functionality? > > My use case is a native widget that is called via Tcl/Tk and exposed > via Tk bindings--there is no easy way to pass Python or Ruby calls to > this widget. "Register" allows me to pass a Python function that is > executed as if it were Tcl code. I would love similar functionality in > Ruby. There are some ways of interaction betwenn Ruby and Tcl. Followings are some of useful methods to do that. -------------------------------------------------------- Tk.install_cmd(proc_or_method) : get Tcl-command string to call Ruby's proc (or method) from Tcl interp. : ruby_proc must be an object of Proc, Method, or TkCallbackEntry. Tk.ip_eval(tcl_script_string) : eval Tcl's script on Tcl interp. Tk.ip_invoke(tcl_cmd_name, tcl_cmd_arg, ...) : call Tcl command with args. -------------------------------------------------------- For example, -------------------------------------------------------- $ irb19 irb(main):001:0> require 'tk' => true irb(main):002:0> tcl_cmd = Tk.install_cmd(proc{|*args| p [:foo, args]}) => "rb_out c00001" irb(main):003:0> Tk.ip_eval("proc foo_cmd {} {#{tcl_cmd} 1 2 3}") => "" irb(main):004:0> exit [nagai@orca17 ~]$ irb20 irb(main):001:0> require 'tk' => true irb(main):002:0> tcl_cmd = Tk.install_cmd(proc{|*args| p [:foo, args]}) => "rb_out c00001" irb(main):003:0> Tk.ip_eval(tcl_cmd + " a b c d") [:foo, ["a", "b", "c", "d"]] => "foo {a b c d}" irb(main):004:0> Tk.ip_eval("proc foo_cmd {} {#{tcl_cmd} 1 2 3}") => "" irb(main):005:0> Tk.ip_invoke("foo_cmd") [:foo, ["1", "2", "3"]] => "foo {1 2 3}" irb(main):006:0> -------------------------------------------------------- -- Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp) Department of Artificial Intelligence, Kyushu Institute of Technology