Ruby 1.9 : What is the use for Proc#yield

G

Gregory Brown

I'm reading through Mauricio's list of changes[0] to Ruby 1.9 and
having trouble with understanding the point of Proc#yield

---

Invokes the block, setting the block's parameters to the values in
params in the same manner the yield statement does.

a_proc.yield(9, 1, 2, 3) #=> [9, 18, 27]
a_proc.yield([9, 1, 2, 3]) #=> [9, 18, 27]
a_proc = Proc.new {|a,b| a}
a_proc.yield(1,2,3) # => [1]

---

It seems like this is the same as Proc#[] or Proc#call
VERSION => "1.8.4"
a_proc = Proc.new {|a, *b| b.collect {|i| i*a }}
=> # said:
a_proc[9,1,2,3] => [9, 18, 27]
a_proc[[9,1,2,3]] => [9, 18, 27]
a_proc = Proc.new { |a,b| a }
=> # said:
a_proc[1,2,3]
=> 1

Mauricio says "Proc#yield was added (also NilClass#yield which raises
a LocalJumpError so you can use it on &block)."

The best 'feature' I can think of is that this lets you do

def something(&block)
block.yield(1,2,3)
rescue LocalJumpError
puts "was expected a block"
end

But I'm not sure I'm convinced of how cool that is. Am I missing something?
 
G

Gregory Brown

Hi,

In message "Re: Ruby 1.9 : What is the use for Proc#yield"

|I'm reading through Mauricio's list of changes[0] to Ruby 1.9 and
|having trouble with understanding the point of Proc#yield

It's mere alias to call, just to describe the intention to invoke it
as a block passed.

Thanks for clarifying this.

-greg
 

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
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top