Currently I'm reading "The Well-Grounded Rubyist" by David A. Black, and I stuck at 10.9 chapter (Enumerators and the next dimension of enumerability). My question is about yield
method.
What is the meaning of the word yield
in Ruby context? My native language is Russian, and Google Translate shows me a bunch of translation variants, that are confusing me. There are some of them: 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 yield
keyword itself.
UPD_2: I've found interesting article about Enumerators: "Lazy Enumerators in Ruby".
"Yield" refers to the earnings generated and realized on an investment over a particular period of time. It's expressed as a percentage based on the invested amount, current market value, or face value of the security. Yield includes the interest earned or dividends received from holding a particular security.
The apple/peach trees yielded an abundant harvest. This soil should yield good crops. The seeds yield a rich oil.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With