From: 7stud -- Date: 2007-11-01T03:52:31+09:00 Subject: Re: .each do |foo, bar| what does bar do? 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. >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) ???? 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. -- Posted via http://www.ruby-forum.com/.