how to escape variable expansion in a block

J

Joachim Wuttke

Dear experts:
I am not happy with the following code fragment.
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 ?
Regards, Joachim


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
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: how to escape variable expansion in a block"

|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.
 
R

Robert Klemme

Yukihiro Matsumoto said:
Hi,

In message "Re: how to escape variable expansion in a block"
|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.

What about

TkButton.new( frame ) {
text 'press here'
command proc { self.buttonPressed }
}

?

robert
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: how to escape variable expansion in a block"

|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.
 
R

Robert Klemme

Yukihiro Matsumoto said:
Hi,

In message "Re: how to escape variable expansion in a block"
|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.

Ah, ok! Now it's clear why the additional var "this" was mentioned.
Sorry for the noise.

Kind regards

robert
 
H

Hidetoshi NAGAI

Hi,

From: "Joachim Wuttke" <[email protected]>
Subject: how to escape variable expansion in a block
Date: Mon, 4 Oct 2004 05:33:09 +0900
Message-ID: said:
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'.
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top