20

Currently, I'm reading "The Well-Grounded Rubyist" by David A. Black, and I'm stuck at chapter 10.9 (Enumerators and the next dimension of enumerability). My question is about the yield method. What is the meaning of the word yield in the Ruby context? My native language is Russian, and Google Translate shows me a bunch of translation variants, that are confusing me. Some of them are: give, bring, surrender (give up), produce, agree, comply and many others.

UPD: please, pay attention to the fact, that I'm trying to understand the meaning of the Enumerator::Yielder#yield method, but not the yield keyword itself.

UPD_2: I've found an interesting article about Enumerators: "Lazy Enumerators in Ruby".

kyrylo
  • 1,691
  • 1
  • 15
  • 22
  • @JeffH: The OP is asking about the `yield` method, not the `yield` keyword. – Jörg W Mittag Feb 21 '11 at 19:44
  • @Jörg W Mittag: I see that now. I amended my answer. – JeffH Feb 21 '11 at 19:53
  • 2
    Without meaning to criticise you, it might have been a good idea to mention you weren't talking about the `yield` keyword - I've programmed in Ruby for over two years, and I haven't actually used the `yield` method. – Andrew Grimm Feb 21 '11 at 21:53
  • 2
    Very interesting question! I am a not an english native speaker myself and I am stuck with this word as well (while reading the same book), which is not well translated in french. – Amokrane Chentir Mar 16 '11 at 16:03

6 Answers6

26

The word yield doesn't really have any special meaning in the context of Ruby. It means the same thing as it means in every other programming language, or in programming and computer science in general.

It is usually used when some kind of execution context surrenders control flow to a different execution context. For example, in Unix, there is a sched_yield function which a thread can use to give up the CPU to another thread (or process). With coroutines, the term yield is generally used to transfer control from one coroutine to another. In C#, there is a yield keyword, which is used by an iterator method to give up control to the iterating method.

And in fact, this last usage is exactly identical to the usage of the Enumerator::Yielder#yield method in Ruby, which you were asking about. Calling this method will suspend the enumerator and give up control to the enumerating method.

Example:

fibs = Enumerator.new do |y|
  a, b = 0, 1
  y.yield a
  loop do
    y.yield b
    a, b = b, a + b
  end
end

puts fibs.next #  0
puts fibs.next #  1
puts fibs.next #  1
puts fibs.next #  2
puts fibs.next #  3
puts fibs.next #  5
puts fibs.next #  8
puts fibs.next # 13
puts fibs.next # 21

As you see, there is an infinite loop. Obviously, if this loop just ran on its own, it wouldn't be of much use. But since every time it hits the yield method, it gives up control until it is called again, this will produce the Fibonacci numbers one by one, essentially representing an infinitely long list of all Fibonacci numbers.

There is another method, Fiber.yield, which serves a similar purpose. (In fact, I already described it above, because Fiber is just Ruby's name for coroutines.) Inside a Fiber, you call Fiber.yield to give up control back to the execution context that originally gave control to you.

Lastly, there is the yield keyword, which is used inside a method body to give up control to the block that was passed into the method.

Note that, at least in the Enumerator case (i.e. the first example), you can additionally interpret yield as to produce, since the Enumerator produces a new value, every time it calls yield.

Community
  • 1
  • 1
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • +1 for the Comp Sci context and example that directly answers OP. – JeffH Feb 21 '11 at 20:39
  • When you say "The word yield doesn't really have any special meaning in the context of Ruby. It means the same thing as it means in every other programming language, or in programming and computer science in general.", you're referring to the `yield` method, as opposed to when you said "The usage of the yield keyword in Ruby has nothing whatsoever to do with the usual CS definition of yield." about the keyword [here](http://stackoverflow.com/questions/764134/rubys-yield-feature-in-relation-to-computer-science/764152#764152), right? – Andrew Grimm Feb 21 '11 at 22:12
  • @Andrew Grimm: Actually, it looks more like I changed my mind. I'll have to think about that. – Jörg W Mittag Feb 22 '11 at 02:16
  • Considering that in almost all languages that have `yield`, it does something very different *"It means the same thing as it means in every other programming language"* doesn't mean much. xd – ndnenkov Dec 12 '16 at 14:45
3

In the context of yielding in an Enumerator, the meaning is closest to "bring forth." The Enumerator calls the yield method of its yielder object, which "brings forth" any value passed to it.

give_me = Enumerator.new do |yielder|
  (1..5).each do |n|
    yielder.yield n
  end
end

5.times do
  p give_me.next
end

This results in:
1
2
3
4
5

In the case of yielding to a block, the meaning is closest to "surrender." The method with the yield statement surrenders execution to whatever block you passed to that method.

def wage_war
  p "What should I do?"
  yield
end

wage_war { p "Surrender!" }
Jamie Forrest
  • 10,895
  • 6
  • 51
  • 68
3

You may find it amusing to read what Programming Ruby 1.9 has to say about the yield keyword:

Programming-language buffs will be pleased to know that the keyword yield was chosen to echo the yield function in Liskov’s language CLU, a language that is more than thirty years old and yet contains features that still haven’t been widely exploited by the CLU-less.

More reading:

A History of CLU (pdf)

Barbara Liskov (wikipedia)

zetetic
  • 47,184
  • 10
  • 111
  • 119
2

@Prostosuper, the related definition I like best is this one:

concede, cede, grant (give over; surrender or relinquish to the physical control of another)

In @Jamie Forrest's example, when wage_war is called, "What should I do?" is printed first, then flow control is yielded (conceded, ceded, granted, given over) to the block that wage_war was called with, resulting in "Surrender!" being printed. After that block is complete, flow control resumes in wage_war. If there were another statement after the yield, it would be executed when flow control resumed in wage_war after "Surrender!" was printed.

EDIT: @Prostosuper asked about yield as it pertains to Enumerators, not blocks, and my example discusses its use in blocks. A SO question (with answers) about Enumerator::Yielder#yield is here. The sense of the definition still applies.

Community
  • 1
  • 1
JeffH
  • 10,254
  • 2
  • 27
  • 48
0

yield, in the ruby context "brings" :) the block passed as a parameter to your method.

def my_method
    p "I have to say something:"
    yield
end

my_method do
   p "hello world!"
end

prints

I have to say something:
hello world

the code p "hello world" is executed when my_method reaches the yield

Mauricio
  • 5,854
  • 2
  • 28
  • 34
  • -1. The OP is asking about the `yield` method, not the `yield` keyword. – Jörg W Mittag Feb 21 '11 at 19:14
  • yield method? please add a reference. – Mauricio Feb 21 '11 at 19:26
  • @Jörg W Mittag: It is not entirely clear that the OP knows `yield` is normally a keyword. It seems quite likely that the word "method" is an incorrect assumption on prostosuper's part. The Fiber method is fairly obscure and I would not assume that's what is meant unless it's explicitly stated. – Chuck Feb 21 '11 at 19:33
  • Indeed, I doubt the book even introduce the Fiber class, it start talking about Threads in the chapter 14... – Mauricio Feb 21 '11 at 19:40
  • 1
    @Chuck: the OP explicitly talks about `Enumerator`s, not blocks. The `yield` method is part of the `Enumerator` API (more precisely: the `Enumerator::Yielder` API). And the OP *explicitly* mentions the `yield` method. "My question is about yield method." -- I don't know what could be clearer than that. – Jörg W Mittag Feb 21 '11 at 19:42
  • great, that's a reference! Thanx! – Mauricio Feb 21 '11 at 19:43
0

In c# yield in a loop is something like a return keyword and is part of the loop that iterates the list. Why not using return then? Because with yield you give back control to the loop while iterating the values in that list. So may be yield is more like a control mechanism.