From: 7stud -- Date: 2007-11-01T06:50:41+09:00 Subject: Re: .each do |foo, bar| what does bar do? > David A. Black wrote: >>>> 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? > Hash#each returns an array(as my examples showed). You said: >If you were going to write Hash#each in Ruby, without recourse to >#each, So, yes...your example would have to return an array to implement Hash#each. > 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. > Ah. So, it's like the splat operator(*)? With the splat operator you might write: def meth1(*args) #gathers the args into an array meth2(*args) #applied a second time -- explodes the array end def meth2(a, b) p a p b end meth1(1, 2) So, applying & a second time conceptually converts the Proc object back to a block and passes it to the method? > 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. > Whoa. That doesn't seem consistent with your earlier example. Why is proc3 outside the parentheses? It doesn't seem to work: def some_method(proc_1, proc_2) proc_1.call proc_2.call yield 'goodbye' end p1 = Proc.new{puts "hello"} p2 = Proc.new{puts "world"} p3 = Proc.new{|val| puts val} some_method(p1, p2) &p3 --output:-- hello world r6test.rb:5:in `some_method': no block given (LocalJumpError) from r6test.rb:11 While this works: p1 = Proc.new{puts "hello"} p2 = Proc.new{puts "world"} p3 = Proc.new{|val| puts val} some_method(p1, p2, &p3) --output:-- hello world goodbye -- Posted via http://www.ruby-forum.com/.