How to use Enumerator::Generator in Ruby 1.9.2

Discussion in 'Ruby' started by Joey Zhou, Apr 19, 2011.

  1. Joey Zhou

    Joey Zhou Guest

    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/.
    Joey Zhou, Apr 19, 2011
    #1
    1. Advertising

  2. 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/scat.rb/ruby/ruby-talk/381414

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Apr 19, 2011
    #2
    1. Advertising

  3. 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
    Johannes Held, Apr 19, 2011
    #3
  4. On Tue, Apr 19, 2011 at 12:55 PM, Johannes Held
    <-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/
    Robert Klemme, Apr 19, 2011
    #4
  5. Joey Zhou

    Joey Zhou Guest

    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/.
    Joey Zhou, Apr 19, 2011
    #5
  6. Joey Zhou

    7stud -- Guest

    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/.
    7stud --, Apr 19, 2011
    #6
  7. Joey Zhou

    Joey Zhou Guest

    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/.
    Joey Zhou, Apr 20, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmltIEhlYXZleQ==?=

    Char Enumerator

    =?Utf-8?B?SmltIEhlYXZleQ==?=, Jul 16, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    381
    bruce barker
    Jul 16, 2004
  2. Angelos Karantzalis

    DataGrid: Bind to an Enumerator of object[]'s

    Angelos Karantzalis, Nov 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    439
  3. Oliver Hopton

    Collection class bound to DataGrid doesn't use my custom enumerator!

    Oliver Hopton, Oct 1, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    123
    Alan Green
    Oct 29, 2003
  4. Artem Voroztsov

    Enumerator in ruby 1.9.

    Artem Voroztsov, Mar 13, 2008, in forum: Ruby
    Replies:
    7
    Views:
    127
    Artem Voroztsov
    Mar 13, 2008
  5. Mischa Fierer

    Help using ruby enumerator idioms

    Mischa Fierer, Jan 1, 2009, in forum: Ruby
    Replies:
    1
    Views:
    84
    Mike Gold
    Jan 2, 2009
Loading...

Share This Page