From: Jean-Hugues ROBERT Date: 2004-05-03T13:45:45+09:00 Subject: Re: Why no Proc##[]=() ? Why no Proc##replace() ? At 22:10 02/05/2004 +0900, you wrote: >On Sun, May 02, 2004 at 05:18:09PM +0900, Jean-Hugues ROBERT wrote: > > There are other ways to do this of course, without replace: > > p = proc { @x ||= long_method } > > or (needed if result can be nil) > > p = proc { @x = long_method unless defined? @x; @x } > >It seems to me that you're assuming @x is an instance variable of the >Proc object, which it is not: You are right, I was wrong. I believed @x was an instance variable of Proc object. >def foo; 1 end ># =>nil >p = proc { @x ||= foo } ># =># >@x ># => (irb):3: warning: instance variable @x not initialized ># =>nil >p[] ># =>1 >@x ># =>1 Hum... OK. I am wrong. How can I have a block variable that survives multiple across multiple invocation ? I had tried with a local variable, but that did not work. So I tried with an instance variable, and it seemed to work (but does not, because I was wrong assuming self was the Proc object, whereas in fact it is the enclosing method's self, which makes sense BTW). BTW: Where can I save the result of long_method, if neither in a class variable, nor in a local variable ? Is there such a thing that "current_block()" or whatever method that tells me what the Proc object is ? I guess my example with self.replace { mutated_block } was wrong too (it was not supposed to work anyways because replace is not there for Proc/s). So... How can a block remember things ? Yours, Jean-Hugues >-- >Running Debian GNU/Linux Sid (unstable) >batsman dot geo at yahoo dot com > >'Ooohh.. "FreeBSD is faster over loopback, when compared to Linux >over the wire". Film at 11.' > -- Linus Torvalds ------------------------------------------------------------------------- Web: http://hdl.handle.net/1030.37/1.1 Phone: +33 (0) 4 92 27 74 17