Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3181

Re: How to use Enumerator::Generator in Ruby 1.9.2

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!talisker.lacave.net!lacave.net!not-for-mail
From 7stud -- <bbxx789_05ss@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: How to use Enumerator::Generator in Ruby 1.9.2
Date Tue, 19 Apr 2011 12:36:55 -0500
Organization Service de news de lacave.net
Lines 45
Message-ID <950f194d1e76caf56b989a7d3d5b73ef@ruby-forum.com> (permalink)
References <9bf8d8ed06528ddc8ebda9e54b79e232@ruby-forum.com> <02a9a5a3a445776b6e7bbeba665d8bc9@ruby-forum.com>
NNTP-Posting-Host bristol.highgroove.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace talisker.lacave.net 1303234638 46732 65.111.164.187 (19 Apr 2011 17:37:18 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Tue, 19 Apr 2011 17:37:18 +0000 (UTC)
In-Reply-To <02a9a5a3a445776b6e7bbeba665d8bc9@ruby-forum.com>
X-Received-From This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway
X-Mail-Count 381864
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <950f194d1e76caf56b989a7d3d5b73ef@ruby-forum.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:3181

Show key headers only | View raw


Joey Zhou wrote in post #993744:
> e = Enumerator.new do |y|
>   y << 1
> end
>
> p e #=> #<Enumerator: #<Enumerator::Generator:0x1345b00>:each>
>
> well, it seems that e is an Enumerator, but has an Enumerator::Generator
> in it. I am confused what on earth Enumerator::Generator is.
>

It's that y thing, and it knows how to provide values to the enumerator 
when your code requests values from the enumerator.


> Can I get
> an object that can return "Enumerator::Generator" if I type "obj.class"?
>

Let's see:

1)
obj = Enumerator::Generator.new
puts obj.class

--output:--
prog.rb:1:in `initialize': no block given (LocalJumpError)
  from prog.rb:1:in `new'
  from prog.rb:1:in `<main>'

2)
obj = Enumerator::Generator.new do |y|
  y << 1
end

puts obj.class

--output:--
Enumerator::Generator

There you go.

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to use Enumerator::Generator in Ruby 1.9.2 Joey Zhou <yimutang@gmail.com> - 2011-04-19 01:43 -0500
  Re: How to use Enumerator::Generator in Ruby 1.9.2 Robert Klemme <shortcutter@googlemail.com> - 2011-04-19 02:44 -0500
    Re: How to use Enumerator::Generator in Ruby 1.9.2 Johannes Held <johannes.held@informatik.uni-erlangen.de> - 2011-04-19 12:55 +0200
      Re: How to use Enumerator::Generator in Ruby 1.9.2 Robert Klemme <shortcutter@googlemail.com> - 2011-04-19 07:01 -0500
  Re: How to use Enumerator::Generator in Ruby 1.9.2 Joey Zhou <yimutang@gmail.com> - 2011-04-19 07:49 -0500
    Re: How to use Enumerator::Generator in Ruby 1.9.2 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-19 12:36 -0500
  Re: How to use Enumerator::Generator in Ruby 1.9.2 Joey Zhou <yimutang@gmail.com> - 2011-04-19 21:23 -0500

csiph-web