Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to use Enumerator::Generator in Ruby 1.9.2

Reply
Thread Tools

How to use Enumerator::Generator in Ruby 1.9.2

 
 
Joey Zhou
Guest
Posts: n/a
 
      04-19-2011
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/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      04-19-2011
On Tue, Apr 19, 2011 at 8:43 AM, Joey Zhou <> 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...by-talk/381414

Cheers

robert

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

 
Reply With Quote
 
 
 
 
Johannes Held
Guest
Posts: n/a
 
      04-19-2011
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...by-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
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      04-19-2011
On Tue, Apr 19, 2011 at 12:55 PM, Johannes Held
<> 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...by-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/

 
Reply With Quote
 
Joey Zhou
Guest
Posts: n/a
 
      04-19-2011
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/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      04-19-2011
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/.

 
Reply With Quote
 
Joey Zhou
Guest
Posts: n/a
 
      04-20-2011
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
lib/ruby/1.8/e2mmap.rb:51: Use Ruby 1.1 (RuntimeError) - WindowsVista Ruby 1.8.6 Zeno Davatz Ruby 2 01-12-2011 09:31 AM
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
Still use 'ruby-bugs' for Ruby bugs? Josef 'Jupp' Schugt Ruby 2 11-04-2004 10:10 PM
Re: OT: Ruby programmers use Python (was Re: ruby on rails ...python on ?) Ed Leafe Python 1 11-02-2004 01:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57