From: Gennady Bystritsky Date: 2006-08-02T01:16:39+09:00 Subject: Re: I thought this was the one that worked? Yes, it is: class B def initialize(a) @a = a end def each yield # First block invocation @a.z = 77 # Changing the scope in class << @a # which the block was created def puts(*args) $stderr.puts @z, *args.map { |_a| _a.upcase } end end yield # Second block invocation end end class A attr_accessor :z def f array = B.new(self) ### HERE'S YOUR FRAGMENT array.each do || puts "Hello world!" end ### YOUR FRAGMENT ENDS HERE end end A.new.f Procuces: Hello world! 77 HELLO WORLD! Ruby does not assume what a method is going to do with a block, it just creates a closure. Gennady. > -----Original Message----- > From: Chad Perrin [mailto:perrin@apotheon.com] > Sent: Tuesday, August 01, 2006 3:32 AM > To: ruby-talk ML > Subject: Re: I thought this was the one that worked? > > On Tue, Aug 01, 2006 at 06:15:10PM +0900, Just Another Victim > of the Ambient Morality wrote: > > > > Well, I think that meshes just fine with my take on it... > > To tell you the truth, I didn't expect you to ask me what my > > understanding of a closure is because I thought I had > already demonstrated > > it with the code sample I gave. Of course, you're busy > digesting that and I > > appreciate it. It means you're actually trying to > understand me (rather > > than just arguing at me. Something that happens all too often...). > > If I may be so bold, I think that, perhaps, blocks > don't look like > > closures to you because you don't realize that methods that > take blocks, > > like "each," "inject," "collect," etc.. are methods (member > functions, if > > you're a C++ guy) and not arbitrary language constructs, > like "if," "while," > > "for," etc... > > I'm not exactly a C++ guy. I suppose I'm more of a Perl guy than > anything else, judging by my familiarity with languages and the > assumption of a lack of loathing (I'm as familiar with PHP as > with Perl, > but I don't like it very much, for instance). I am familiar with many > of the idioms and much of the semantic and syntactic theory > of a lot of > languages, even if I don't use them much, though. The > dangers of doing > consulting and industry analysis for money. . . . > > > > > > Now that I think about it, this whole argument can be > settled with one > > simple question. If you doubt that all blocks are closures > then show us a > > counter-example. Show us a "block" that is not a closure > and I will show > > you how it's not a block... > > Is this a closure? You tell me (and tell me how, if it is): > > array.each do || > puts "Hello world!" > end > > -- > CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] > print substr("Just another Perl hacker", 0, -2); > >