Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to escape variable expansion in a block

0 views
Skip to first unread message

Joachim Wuttke

unread,
Oct 3, 2004, 4:33:09 PM10/3/04
to
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


Yukihiro Matsumoto

unread,
Oct 4, 2004, 6:01:41 AM10/4/04
to
Hi,

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.

Robert Klemme

unread,
Oct 4, 2004, 6:35:30 AM10/4/04
to

"Yukihiro Matsumoto" <ma...@ruby-lang.org> schrieb im Newsbeitrag
news:1096884099.590405...@x31.priv.netlab.jp...

What about

TkButton.new( frame ) {
text 'press here'

command proc { self.buttonPressed }
}

?

robert

Yukihiro Matsumoto

unread,
Oct 4, 2004, 7:58:25 AM10/4/04
to
Hi,

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.


Robert Klemme

unread,
Oct 4, 2004, 8:09:13 AM10/4/04
to

"Yukihiro Matsumoto" <ma...@ruby-lang.org> schrieb im Newsbeitrag
news:1096891101.544558...@x31.priv.netlab.jp...

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

Kind regards

robert

Hidetoshi NAGAI

unread,
Oct 4, 2004, 10:22:40 AM10/4/04
to
Hi,

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)


0 new messages