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

D

David A. Black

Hi --

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

--
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!
 
7

7stud --

David said:
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
 
D

David A. Black

Hi --

Hash#each returns an array(as my examples showed). You said:


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.
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?

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
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

--
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!
 
D

David A. Black

Hi --

David A. Black wrote:
.

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

--
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!
 
7

7stud --

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top