Ruby/tk internals...?

Hi there,

I am trying to learn how to glue tk with a scripting language, and have
several things that I cannot figure out.

I have understand that in tcltklib.c a tcl command ‘ruby’ is defined,
which executes it’s argument as a ruby command. In tk.rb, a
TkCore.callback method is defined, which is not clear for me. I see that
there is some Tk_CMDTBL and ‘rb_out …’ magic strings, but it is a bit
messy for me. :frowning:

In practice, I would like to know that the callbacks I define, in what
context will run?

I have tried to figure it out:

···

irb(main):001:0> require "tk"
true
irb(main):004:0> Tk::INTERP._invoke “ruby”,"id"
TypeError: failed to convert Fixnum into String
from /usr/lib/ruby/1.6/tk.rb:889:in _invoke' from /usr/lib/ruby/1.6/tk.rb:889:in_invoke’
from (irb):4
irb(main):002:0> Tk::INTERP._invoke “ruby”,“id.to_s”
"538134128"
irb(main):003:0> self.id
538134128
irb(main):005:0> a=“blah”
"blah"
irb(main):007:0> Tk::INTERP._invoke “ruby”,"a"
NameError: (eval):889:in _invoke': undefined local variable or methoda’ for #Object:0x40268ce0
from (eval):889:in _invoke' from /usr/lib/ruby/1.6/tk.rb:889:in_invoke’
from (irb):7
irb(main):008:0> Tk::INTERP._invoke “ruby”,“a=‘blahblah’”
"blahblah"
irb(main):009:0> Tk::INTERP._invoke “ruby”,“a"
NameError: (eval):889:in _invoke': undefined local variable or methoda’ for #Object:0x40268ce0
from (eval):889:in _invoke' from /usr/lib/ruby/1.6/tk.rb:889:in_invoke’
from (irb):9
irb(main):010:0> a
"blah”

In short, it look like the string is evaled in the context of main, but
the local variables are not seen.

Could somebody help to clear it out to me? :slight_smile:

Thanks:
Circum

Hi,

···

In message “ruby/tk internals…?” on 02/08/15, Ferenc Engard engard@all.hu writes:

In practice, I would like to know that the callbacks I define, in what
context will run?

The argument string is evaluated under the context created for each
invocation of “ruby” command.

						matz.

In practice, I would like to know that the callbacks I define, in what
context will run?

The argument string is evaluated under the context created for each
invocation of “ruby” command.

And what is this context? Is it a plain new Object for every invocation,
i.e. I can use only global variables and methods defined in main, cannot
define new methods or classes, etc.?

Anyway, at the URL 'http://www.perfectxml.com/syngress/ruby/Page1.asp
there is a short description about many GUIs, and it writes, that in
Ruby/GTK the signal handlers have context of its caller. But the article
doesn’t tell what is that context:

To associate a signal from a widget with some specific action, you can
call the widget’s signal_connect method. This method takes a string
argument indicating the signal name (like “clicked”) and a code block
that will be evaluated in the caller’s context. For example, if your
Ruby/GTK-based spreadsheet program should save the spreadsheet’s
contents whenever the Save button is clicked, you might include the
lines:

saveButton.signal_connect(‘clicked’) {
saveSpreadsheetContents if contentsModified?
}
<<

How does it work with GTK?

Circum

Hi,

The argument string is evaluated under the context created for each
invocation of “ruby” command.

And what is this context? Is it a plain new Object for every invocation,
i.e. I can use only global variables and methods defined in main, cannot
define new methods or classes, etc.?

It is a plain new “scope” for every invocation. You can define new
methods or classes, but I do recommend you to keep the argument to the
"ruby" command small.

How does it work with GTK?

The context is saved in the Proc object. It is same for Ruby/Tk, for
example,

require “tk”

TkButton.new(nil,
‘text’ => ‘hello’,
‘command’ => proc{print “hello\n”}).pack(‘fill’=>‘x’)
TkButton.new(nil,
‘text’ => ‘quit’,
‘command’ => ‘exit’).pack(‘fill’=>‘x’)

The callback (print “hello\n”) is executed under the current context.
“ruby” command is used to trigger the callback.

						matz.
···

In message “Re: ruby/tk internals…?” on 02/08/15, Ferenc Engard engard@all.hu writes: