From: "David A. Black" Date: 2007-11-01T04:16:51+09:00 Subject: Re: .each do |foo, bar| what does bar do? Hi -- On Thu, 1 Nov 2007, 7stud -- wrote: > David A. Black wrote: >> Hi -- >> >> On Sun, 28 Oct 2007, 7stud -- wrote: >> >>>> class Hash >>>> def each >>>> each_key {|key| yield key, self[key] } >>>> end >>>> end >>>> >>> >>> >>> That suffers the same problem as David Black's example. >> >> What problem did mine suffer from? >> > > It doesn't return an array. Was it supposed to? >> def each(&block) >> orig_each(&block) >> >> versus >> >>> orig_each(block) >>> >>> According to pickaxe2, p56, the '&' method converts the specified block >>> to a Proc object and assigns it to the parameter variable 'block'. Why >>> is the second call to '&' required? >> >> Because there's a difference between passing a Proc around as an >> object, and supplying a code block to a method. You can do both: >> >> meth(arg,&block) >> >> and arg can be a Proc object. So there has to be some way to tell the >> method what you're doing. >> > > I'm not getting it. With this definition: > > def each(&a_block) > > when I call: > > each() {some block} > > ruby converts the block to a Proc object and assigns it to the variable > a_block. So, it seems to me that after ruby passes the args specified > in the method call to each(), the block would no longer be accessible > inside the method--only the Proc object assigned to a_block would be > accessible. Are you saying that when the next line executes: > >> def each(&a_block) >> orig_each(&a_block) <----**** > > that the parameter variable a_block in the line: > >> orig_each(&a_block) > > is not the same variable as the a_block in the line: > >> def each(&a_block) No, they're the same. > ???? In other words, does a_block in the line: > > >> orig_each(&a_block) > > > reach outside the method definition and reference the block that is > floating around in the ether? Does ruby re-convert the block into a > Proc object and re-assigns the Proc object to a_block? If not, I don't > understand why the second '&' is necessary: writing a_block should be > enough to access the Proc object that ruby assigned to the parameter > variable a_block when the method was first called. It does access the Proc object. But accessing the Proc object is only part of the story; there's also the question of what it does with the Proc object. If you do this: some_method(some_proc_object) you're just passing the Proc object around the same way you would pass a string or an array or any other object. If you do this: some_method &some_proc_object you're telling some_method that you want some_proc_object to play the special role of code-block. There could even be a situation where you would do: some_method(proc_1, proc_2) &proc_3 i.e., send two procs as regular arguments, and use a third one as the code block. David -- Upcoming training by David A. Black/Ruby Power and Light, LLC: * Advancing With Rails, Edison, NJ, November 6-9 * Advancing With Rails, Berlin, Germany, November 19-22 * Intro to Rails, London, UK, December 3-6 (by Skills Matter) See http://www.rubypal.com for details!