R
Ronald Fischer
Assume that I'm modelling a visible form, consisting of entry fields,
buttons etc., as a class:
require 'tk'
class Form
def initialize(...)
@foo=1234
...
@root=TkRoot.new() { title "My Form" }
action_button=TkButton.new(@root) {
text "Action!"
command {
# Do something with @root and @foo
puts @root.foo # DOES NOT WORK
}
}
action_button.pack("side" => "right");
end
end
The problem is with the command routine inside the action_button:
Both @root and @foo are undefined here. Obviously, the class context
is not present anymore inside this callback function.
How would one model this situation in Ruby? In C++ terms: How do
I pass the 'this' pointer of my class into the command callback
of the button?
In my case, I happen to have only one instance of the Form, so
I could use a global variable, $form, to hold a reference to my
Form object and access it via this global, but of course this
solution would be ugly beyond imagination...
Kind regards,
Ronald
buttons etc., as a class:
require 'tk'
class Form
def initialize(...)
@foo=1234
...
@root=TkRoot.new() { title "My Form" }
action_button=TkButton.new(@root) {
text "Action!"
command {
# Do something with @root and @foo
puts @root.foo # DOES NOT WORK
}
}
action_button.pack("side" => "right");
end
end
The problem is with the command routine inside the action_button:
Both @root and @foo are undefined here. Obviously, the class context
is not present anymore inside this callback function.
How would one model this situation in Ruby? In C++ terms: How do
I pass the 'this' pointer of my class into the command callback
of the button?
In my case, I happen to have only one instance of the Form, so
I could use a global variable, $form, to hold a reference to my
Form object and access it via this global, but of course this
solution would be ugly beyond imagination...
Kind regards,
Ronald