# Generator#yield - what does it do?

**URL:** <https://www.ruby-forum.com/t/generator-yield-what-does-it-do/121928>\
**Category:** Ruby\
**Created:** [December 3, 2007, 12:03am UTC](https://www.ruby-forum.com/t/generator-yield-what-does-it-do/121928 "2007-12-03T00:03:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [December 3, 2007, 12:03am UTC](https://www.ruby-forum.com/t/generator-yield-what-does-it-do/121928/1 "2007-12-03T00:03:58Z")

</div>

Hi!

I am confused somehow with respect to “Generator#yield”. The description  
(ri) is…

> ri Generator#yield  
> -------------------------------------------------------- Generator#yield  
> yield(value)

* * *

```
 Yields an element to the generator.

```

It’s unclear for me what this means. I would expect that the Generator  
object delivers (yields) values by requirement through  
“Generator#current” or “Generator#next”, but what should be yielded to  
an already existing Generator object?

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [December 3, 2007, 3:11am UTC](https://www.ruby-forum.com/t/generator-yield-what-does-it-do/121928/2 "2007-12-03T03:11:14Z")

</div>

On Dec 2, 5:03 pm, Wolfgang Nádasi-Donner [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> Yields an element to the generator.
> 
> ```
> 
> It’s unclear for me what this means. I would expect that the Generator  
> object delivers (yields) values by requirement through  
> “Generator#current” or “Generator#next”, but what should be yielded to  
> an already existing Generator object?
> 
> ## Wolfgang Nádasi-Donner
> 
> Posted viahttp://www.ruby-forum.com/.

It’s for passing items _into_ the generator’s queue when the generator  
is created with the block form of Generator#new rather than from an  
existing enumerable. Example…

require ‘generator’  
g = Generator.new { | g |  
1.upto(9) { | i |  
g.yield(i)  
}  
g.yield(10)  
}  
until g.end?  
p g.next  
end

It probably should be spelled “inject” or something.

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [December 3, 2007, 3:17am UTC](https://www.ruby-forum.com/t/generator-yield-what-does-it-do/121928/3 "2007-12-03T03:17:41Z")

</div>

On Dec 2, 8:09 pm, MonkeeSage [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Wolfgang Nádasi-Donner  
> > g.yield(i)  
> > }  
> > g.yield(10)}
> 
> until g.end?  
> p g.next  
> end
> 
> It probably should be spelled “inject” or something.

err… ^^^^^^^^ = “insert”

---

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [December 3, 2007, 8:13am UTC](https://www.ruby-forum.com/t/generator-yield-what-does-it-do/121928/4 "2007-12-03T08:13:05Z")

</div>

Jordan Callicoat wrote:

> On Dec 2, 5:03 pm, Wolfgang Nï¿½dasi-Donner [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > It’s unclear for me what this means. I would expect that the Generator  
> > object delivers (yields) values by requirement through  
> > “Generator#current” or “Generator#next”, but what should be yielded to  
> > an already existing Generator object?  
> > It’s for passing items _into_ the generator’s queue when the generator  
> > is created with the block form of Generator#new rather than from an  
> > existing enumerable. Example…

I see - the confusion came from the usual meaning of yield: call a block  
and deliver objects to it. Here it is used from a block into a method.

btw. - at least in “PickAxe” the word “yield” it marked as a reserved  
word.

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [December 3, 2007, 8:18am UTC](https://www.ruby-forum.com/t/generator-yield-what-does-it-do/121928/5 "2007-12-03T08:18:41Z")

</div>

Hi –

On Mon, 3 Dec 2007, Wolfgang NÃ¡dasi-Donner wrote:

> I see - the confusion came from the usual meaning of yield: call a block  
> and deliver objects to it. Here it is used from a block into a method.
> 
> btw. - at least in “PickAxe” the word “yield” it marked as a reserved  
> word.

Reserved words can still be method names (e.g., class). It would be  
nice to have a different word for this method, since it does feel a  
bit backwards in relation to the other “yield”, but as I recall there  
have been discussions already and nothing that Matz liked was  
proposed.

David

---

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [December 3, 2007, 9:25am UTC](https://www.ruby-forum.com/t/generator-yield-what-does-it-do/121928/6 "2007-12-03T09:25:14Z")

</div>

David A. Black wrote:

> Reserved words can still be method names (e.g., class). It would be  
> nice to have a different word for this method, since it does feel a  
> bit backwards in relation to the other “yield”, but as I recall there  
> have been discussions already and nothing that Matz liked was  
> proposed.

I think the documentation of the method should be a little bit extended.  
The name of the method is not as bad as it looks like in the first  
moment, because it is used in the way to “yield” a value later to the  
user of the generator.

After yours and Jordan’s explanations everything is clear now. It’s  
simply the documentation text “Yields an element to the generator”,  
which leads to the assumption, that the method can be called elsewhere  
and delivers some objekt to the existing generator.

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [December 3, 2007, 8:50am UTC](https://www.ruby-forum.com/t/generator-yield-what-does-it-do/121928/7 "2007-12-03T08:50:51Z")

</div>

On Dec 3, 1:13 am, Wolfgang NÃ¡dasi-Donner [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I see - the confusion came from the usual meaning of yield: call a block  
> and deliver objects to it. Here it is used from a block into a method.

Yes… it really should be spelled differently. The idea is “send this  
item into the generator queue, so that the generator yields it (normal  
meaning) on the next iteration”…but it is confusing to have it named  
yield.

> btw. - at least in “PickAxe” the word “yield” it marked as a reserved  
> word.

Object attributes can share the same name as reserved words since they  
live in the namespace of the caller. Example…

class Control  
def if(cond, yes, no) # ‘if’ is a reserved keyword  
if instance\_eval(cond) then yes else no end  
end  
end  
Control.new.if(“5\>4”, “ok”, “broken”)

# =\> “ok”

> ## Wolfgang NÃ¡dasi-Donner
> 
> Posted viahttp://www.ruby-forum.com/.

Regards,  
Jordan
