Forum: Ruby Ruby 1.9 : What is the use for Proc#yield

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Gregory B. (Guest)
on 2007-06-24 02:12
(Received via mailing list)
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 }}
=> #<Proc:0x00317930@(irb):8>
>> 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 }
=> #<Proc:0x0030a44c@(irb):11>
>> 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?
Gregory B. (Guest)
on 2007-06-24 02:14
(Received via mailing list)
On 6/23/07, Gregory B. <removed_email_address@domain.invalid> wrote:
> I'm reading through Mauricio's list of changes[0] to Ruby 1.9 and
> having trouble with understanding the point of Proc#yield

whoops, forgot to link.

[0] http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9
Yukihiro M. (Guest)
on 2007-06-24 18:53
(Received via mailing list)
Hi,

In message "Re: Ruby 1.9 : What is the use for Proc#yield"
    on Sun, 24 Jun 2007 07:12:20 +0900, "Gregory B."
<removed_email_address@domain.invalid> writes:

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

              matz.
Gregory B. (Guest)
on 2007-06-24 19:35
(Received via mailing list)
On 6/24/07, Yukihiro M. <removed_email_address@domain.invalid> wrote:
> Hi,
>
> In message "Re: Ruby 1.9 : What is the use for Proc#yield"
>     on Sun, 24 Jun 2007 07:12:20 +0900, "Gregory B." 
<removed_email_address@domain.invalid> writes:
>
> |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
This topic is locked and can not be replied to.