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


Groups > comp.lang.ruby > #3144 > unrolled thread

How to use Enumerator::Generator in Ruby 1.9.2

Started byJoey Zhou <yimutang@gmail.com>
First post2011-04-19 01:43 -0500
Last post2011-04-19 21:23 -0500
Articles 7 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  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

#3144 — How to use Enumerator::Generator in Ruby 1.9.2

FromJoey Zhou <yimutang@gmail.com>
Date2011-04-19 01:43 -0500
SubjectHow to use Enumerator::Generator in Ruby 1.9.2
Message-ID<9bf8d8ed06528ddc8ebda9e54b79e232@ruby-forum.com>
Hi everybody,

There's a class called Enumerator::Generator in Ruby 1.9.2, and it seems
to have only one method "#each" of its own.
However the ruby-doc page of Enumerator::Generator is empty, I didn't
google out the useage of it.

So, can you give me an example how to use this class?

Thank you!

Joey

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

[toc] | [next] | [standalone]


#3148

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-19 02:44 -0500
Message-ID<BANLkTin_Ff6FnV6JcPQKQmfc0op_=0Hi7g@mail.gmail.com>
In reply to#3144
On Tue, Apr 19, 2011 at 8:43 AM, Joey Zhou <yimutang@gmail.com> wrote:
> Hi everybody,
>
> There's a class called Enumerator::Generator in Ruby 1.9.2, and it seems
> to have only one method "#each" of its own.
> However the ruby-doc page of Enumerator::Generator is empty, I didn't
> google out the useage of it.
>
> So, can you give me an example how to use this class?

You can see an example in a recent posting:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/381414

Cheers

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#3157

FromJohannes Held <johannes.held@informatik.uni-erlangen.de>
Date2011-04-19 12:55 +0200
Message-ID<9157vaFtmrU1@mid.dfncis.de>
In reply to#3148
On 19.04.2011 09:44, Robert Klemme wrote:
> You can see an example in a recent posting:
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/381414
But this can be achieved easier, as e.g. (0..9).cycle already returns an 
Enumerator.

ruby-1.9.2-p136 :002 > foo = (0..9).cycle
  => #<Enumerator: 0..9:cycle>
ruby-1.9.2-p136 :003 > foo.next
  => 0
ruby-1.9.2-p136 :004 > foo.take 12
  => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
ruby-1.9.2-p136 :005 > foo.next
  => 1
ruby-1.9.2-p136 :006 >

--
Gruß, Johannes

[toc] | [prev] | [next] | [standalone]


#3158

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-19 07:01 -0500
Message-ID<BANLkTi=zFVjovT-_ZA4VKhvZDA2OGE1q3Q@mail.gmail.com>
In reply to#3157
On Tue, Apr 19, 2011 at 12:55 PM, Johannes Held
<johannes.held@informatik.uni-erlangen.de> wrote:
> On 19.04.2011 09:44, Robert Klemme wrote:
>>
>> You can see an example in a recent posting:
>> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/381414
>
> But this can be achieved easier, as e.g. (0..9).cycle already returns an
> Enumerator.

Yes, but please note that #cycle was not used in the example posted by
me which I was referring to.  Any simple example can usually be solved
with another approach than Enumerator so I guess most examples which
are good for conveying functionality of Enumerator.new will suffer
from that very same issue. :-)

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#3163

FromJoey Zhou <yimutang@gmail.com>
Date2011-04-19 07:49 -0500
Message-ID<02a9a5a3a445776b6e7bbeba665d8bc9@ruby-forum.com>
In reply to#3144
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. Can I get
an object that can return "Enumerator::Generator" if I type "obj.class"?

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

[toc] | [prev] | [next] | [standalone]


#3181

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-19 12:36 -0500
Message-ID<950f194d1e76caf56b989a7d3d5b73ef@ruby-forum.com>
In reply to#3163
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/.

[toc] | [prev] | [next] | [standalone]


#3202

FromJoey Zhou <yimutang@gmail.com>
Date2011-04-19 21:23 -0500
Message-ID<4dd610eb5a6ed8425c11236449c7a032@ruby-forum.com>
In reply to#3144
May I think like this, Enumerator.new is a short way, including two
steps:

eg = Enumerator::Generator.new {|y| y << 1 }
p eg # #<Enumerator::Generator:0xb44890>
e1 = Enumerator.new(eg)
p e1 # #<Enumerator: #<Enumerator::Generator:0xb44890>:each>

e2 = eg.to_enum
p e2 # #<Enumerator: #<Enumerator::Generator:0xb44890>:each>

so ruby-doc says there are two forms of Enumerator.new:

Enumerator.new(obj, method = :each, *args)
Enumerator.new { |y| ... }

in fact, there's just one, the second one can be regarded as

Enumerator.new(enum_generator_instance)

Does Enumerator::Generator exist, because Ruby 1.8 has a Generator
class?

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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web