From: Robert Klemme Date: 2012-11-05T21:22:16+09:00 Subject: Re: Yield question --90e6ba10b07d24261204cdbe898d Content-Type: text/plain; charset=ISO-8859-1 On Mon, Nov 5, 2012 at 1:10 PM, Assaf Shomer wrote: > Ruby beginner here. > I noticed the following code in the pickaxe book 1.9 page 101: > --------------------- > class VowelFinder > include Enumerable > def initialize(string) > @string = string > end > def each > @string.scan(/[aeiou]/) do |vowel| > yield vowel > end > end > end > -------------------- > What does it mean to have a *yield* inside a block? I thought yield is > the way a method calls the block. Here the only yield is inside a block > and there is no other method. > The yield is inside #each. It invokes the block passed to method #each. Note this: irb(main):001:0> def t; yield :pre; 2.times {|i| yield i}; yield :post; end => nil irb(main):002:0> t {|x| p x} :pre 0 1 :post => :post As you can see, yield invokes the block no matter where inside method #t it is placed. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --90e6ba10b07d24261204cdbe898d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


On Mon, N= ov 5, 2012 at 1:10 PM, Assaf Shomer <lists@ruby-forum.com> wrote:
Ruby beginner here.
I noticed the following code in the pickaxe book 1.9 page 101:
---------------------
class VowelFinder
=A0include Enumerable
=A0def initialize(string)
=A0 @string =3D string
=A0end
=A0def each
=A0 @string.scan(/[aeiou]/) do |vowel|
=A0 yield vowel
=A0 end
=A0end
end
--------------------
What does it mean to have a *yield* inside a block? I thought yield is
the way a method calls the block. Here the only yield is inside a block
and there is no other method.
<= /font>

The yield is inside #each.=A0 It invokes= the block passed to method #each.=A0 Note this:

irb(main):001:0>= def t; yield :pre; 2.times {|i| yield i}; yield :post; end
=3D> nil
irb(main):002:0> t {|x| p x}
:pre
0
1
:post=3D> :post

As you can see, yield invokes the block no matter wh= ere inside method #t it is placed.

Kind regards

robert



--
remember.guy do |as, often| as.you_c= an - without end
http://b= log.rubybestpractices.com/
--90e6ba10b07d24261204cdbe898d--