Forum: Ruby Yield question

Posted by Assaf Shomer (assafshomer)
on 2012-11-05 13:10
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.
Posted by Robert Klemme (robert_k78)
on 2012-11-05 13:24
(Received via mailing list)
On Mon, Nov 5, 2012 at 1:10 PM, Assaf Shomer <lists@ruby-forum.com> 
wrote:

>   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
Posted by 7stud -- (7stud)
on 2012-11-05 19:32
Assaf Shomer wrote in post #1082905:
> 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?
>

'yield' calls a block.  Which block?  yield calls the block that
was specified when calling 'the method'.  Which method?  The
method that contains the yield statement.  So, work your way outwards
from the yield statement until you find a def statement.  That is 'the
method' whose block will be called by yield.

A block that happens to be *inside* a method is not "the method's
block".  The method's block is specified when you call the method.
So if you see a block inside a method, you know for sure yield
is not calling that block.

> Here the only yield is inside a block
> and there is no other method.

If yield wasn't allowed inside a block(e.g. a loop), which is inside a
method, then you would have to write methods like this:

def do_stuff
  yield 0
  yield 1
  #...
  #...
  yield 998
  yield 999
end


do_stuff {|x| puts x}  #<---The method's block

--output:--
1
2
998
999


But of course it makes sense to be able to do this:

def do_stuff
  1_000.times {|i| yield i}
end

do_stuff {|x| puts x}  #<--- The method's block


--output:--
0
1
...
...
998
999
Posted by Assaf Shomer (assafshomer)
on 2012-11-05 20:23
Got it. Thanks a lot guys for the quick response and succinct 
explanation.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.