From: Robert Klemme Date: 2010-07-22T16:46:02+09:00 Subject: Re: Enumerators and generators 2010/7/22 Hal Fulton : > I'm trying to grasp when/why one might use a generator instead of > an enumerator (or vice versa). It seems the alternative is Generator vs. Enumerable rather than Generator vs. Enumerator. Enumerator is rather a special form of Enumerable which allows to do some things more efficiently because it delays the iteration. E.g. # traditional one liner style: needs much memory: matches = File.readlines("foo").select {|line| /keyword/ =~ line} # Enumerator: only memory for the matches = File.to_enum(:foreach, "foo").select {|line| /keyword/ =~ line} # Same in 1.9 with implicit Enumerator creation matches = File.foreach("foo").select {|line| /keyword/ =~ line} It's a convenient replacement for the equally efficient (memory wise): matches = [] File.foreach "foo" do |line| matches << line if /keyword/ =~ line end Generator is a completely different iteration style as Ricardo explained. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/