class MyClass
def initialize( frame )
...
this = self # <- this is ugly, though it solves the problem
TkButton.new( frame ) {
text 'press here'
command proc { this.buttonPressed } # <- here is the problem
}
...
end
def buttonPressed
puts "here we are"
end
end
__________________________________________________________
Mit WEB.DE FreePhone mit hoechster Qualitaet ab 0 Ct./Min.
weltweit telefonieren! http://freephone.web.de/?mc=021201
In message "Re: how to escape variable expansion in a block"
on Mon, 4 Oct 2004 05:33:09 +0900, "Joachim Wuttke" <wut...@web.de> writes:
|It works, but the introduction of the auxiliary
|variable «this» is not very elegant. Is there another
|way to tell the TkButton constructor that «buttonPressed»
|is a member of the class it was called from ?
Currently, there's no way to avoid. If TkObject had parent attribute,
it could be:
| TkButton.new( frame ) {
| text 'press here'
| command proc { parent.buttonPressed }
| }
Just a possibility.
matz.
What about
TkButton.new( frame ) {
text 'press here'
command proc { self.buttonPressed }
}
?
robert
In message "Re: how to escape variable expansion in a block"
on Mon, 4 Oct 2004 19:39:53 +0900, "Robert Klemme" <bob....@gmx.net> writes:
|What about
|
| TkButton.new( frame ) {
| text 'press here'
| command proc { self.buttonPressed }
| }
|
|?
TkObject initializers swaps self during the execution of the blocks
attached to them, to enable attribution call such as "text" or
"command". If you want to allow self unchanged, we should seek
another model of execution, that might breaks code compatibility.
matz.
Ah, ok! Now it's clear why the additional var "this" was mentioned.
Sorry for the noise.
Kind regards
robert
From: "Joachim Wuttke" <wut...@web.de>
Subject: how to escape variable expansion in a block
Date: Mon, 4 Oct 2004 05:33:09 +0900
Message-ID: <8593...@web.de>
> this = self # <- this is ugly, though it solves the problem
> TkButton.new( frame ) {
> text 'press here'
> command proc { this.buttonPressed } # <- here is the problem
> }
Solution-1:
btn_callback = proc{ buttonPressed }
TkButton.new(frame){
text 'press here'
command btn_callback
}
Solution-2:
b = TkButton.new(frame){text 'press here'}
b.command{ buttonPressed }
Solution-3:
TkButton.new(frame).text('press here').command{ buttonPressed }
Solution-4:
TkButton.new(frame){
text 'press here'
}.command{ buttonPressed }
Solution-5:
TkButton.new(frame, :text=>'press here', :command=>proc{ buttonPressed })
Solution-6:
TkButton.new(frame, :command=>proc{ buttonPressed }){ text 'press here' }
and so on.
Please select the one which you like. :-)
# If I were you, I'll choose 'Solution-5'.
--
Hidetoshi NAGAI (na...@ai.kyutech.ac.jp)