.each do |foo, bar| what does bar do?

Hi –

On Thu, 1 Nov 2007, Brian A. wrote:

Was it supposed to?

I think so, if you want to avoid the “multiple value” warnings shown
in the earlier post. I suppose someone could look at the Ruby source
to see if that’s what it’s actually doing, but I suspect there is a
good chance it is, judging from the behavior. However, I think that
would’ve just cluttered up the example since it’s not germane to your
purpose.

Do you guys mean what it’s yielding, as opposed to what it’s
returning?

David

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

Hi –

On Thu, 1 Nov 2007, 7stud – wrote:

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.

Actually Hash#each returns its receiver, which is a hash. I think my
code also returned self.

some_method &some_proc_object
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?

Yes. I wouldn’t bother comparing it to the *. It’s a way to use a Proc
object as a block. It doesn’t have to be the one that was passed in
originally:

def some_method
p = Proc.new {|x| x * 10 }
return [1,2,3].map(&p)
end

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:

Whoops, that was supposed to be inside, as your tests showed.

David

Hi –

On Thu, 1 Nov 2007, 7stud – wrote:

David A. Black wrote:
.

Do you guys mean what it’s yielding, as opposed to what it’s
returning?

Yes. Sorry.

I’m still not understanding what the problem was with my example. I’m
doing:

yield(key,self[key])

and if you do:

hash.each {|k,v| … }

you should be fine – no warning.

David

David A. Black wrote:
.

Do you guys mean what it’s yielding, as opposed to what it’s
returning?

Yes. Sorry.

David A. Black wrote:

Thanks for the explanation on the &block question.

I’m still not understanding what the problem was with my example. I’m
doing:

yield(key,self[key])

and if you do:

hash.each {|k,v| … }

you should be fine – no warning.

Yes. But there is a problem when you do:

hash.each {|arr| …}

On the other hand, when you write:

hash.each {|arr| …}

with the actual Hash#each method, there is no problem.

On 10/31/07, David A. Black [email protected] wrote:

There could even be a situation where you would do:

some_method(proc_1, proc_2) &proc_3

I think you meant

 some_method(proc_1, proc_2 &proc_3)


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/