no Enumerator#[] ?

Discussion in 'Ruby' started by Roger Pack, Oct 13, 2009.

  1. Roger Pack

    Roger Pack Guest

    Currently

    >> "ab\r\nc".lines[0]

    NoMethodError: undefined method `[]' for #<Enumerator:0x155f220>
    from (irb):1
    from C:/installs/ruby191p243p2/bin/irb:12:in `<main>'

    Doesn't it seem reasonable for this to exist?
    Thanks.
    -r
    --
    Posted via http://www.ruby-forum.com/.
     
    Roger Pack, Oct 13, 2009
    #1
    1. Advertising

  2. Roger Pack

    Patrick Okui Guest

    On Oct 14, 2009, at 12:49 AM, Roger Pack wrote:

    > Currently
    >
    >>> "ab\r\nc".lines[0]

    > NoMethodError: undefined method `[]' for #<Enumerator:0x155f220>
    > from (irb):1
    > from C:/installs/ruby191p243p2/bin/irb:12:in `<main>'
    >
    > Doesn't it seem reasonable for this to exist?


    Try "ab\r\nc".lines.to_a[0].

    --
    patrick
     
    Patrick Okui, Oct 13, 2009
    #2
    1. Advertising

  3. Roger Pack

    trans Guest

    On Oct 13, 6:03=A0pm, Patrick Okui <> wrote:
    > On Oct 14, 2009, at 12:49 AM, Roger Pack wrote:
    >
    > > Currently

    >
    > >>> "ab\r\nc".lines[0]

    > > NoMethodError: undefined method `[]' for #<Enumerator:0x155f220>
    > > =A0 =A0 =A0 =A0from (irb):1
    > > =A0 =A0 =A0 =A0from C:/installs/ruby191p243p2/bin/irb:12:in `<main>'

    >
    > > Doesn't it seem reasonable for this to exist?

    >
    > Try "ab\r\nc".lines.to_a[0].


    Perhaps it does. Why should it have to convert the whole enumerable to
    an array, if it only needs to calculate 0..index? Of course, it might
    be very inefficient to keep asking for lines that way,
    nonetheless... ?
     
    trans, Oct 14, 2009
    #3
  4. Roger Pack

    Guest

    [Note: parts of this message were removed to make it a legal post.]

    2009/10/14 trans <>

    >
    >
    > On Oct 13, 6:03 pm, Patrick Okui <> wrote:
    > > On Oct 14, 2009, at 12:49 AM, Roger Pack wrote:
    > >
    > > > Currently

    > >
    > > >>> "ab\r\nc".lines[0]
    > > > NoMethodError: undefined method `[]' for #<Enumerator:0x155f220>
    > > > from (irb):1
    > > > from C:/installs/ruby191p243p2/bin/irb:12:in `<main>'

    > >
    > > > Doesn't it seem reasonable for this to exist?

    > >
    > > Try "ab\r\nc".lines.to_a[0].

    >
    > Perhaps it does. Why should it have to convert the whole enumerable to
    > an array, if it only needs to calculate 0..index? Of course, it might
    > be very inefficient to keep asking for lines that way,
    > nonetheless... ?
    >
    >

    if you want to get one or two first elements from Enumerator (or other
    Enumerables), you can call
    Enumerable#take

    "ab\r\nc".lines.take(1) => ["ab\r\n"]
    "ab\r\nc".lines.take(2) => ["ab\r\n", "c"]
     
    , Oct 14, 2009
    #4
  5. Roger Pack

    7stud -- Guest

    > Doesn't it seem reasonable for this to exist?

    Well this is ruby...so add away!

    unknown wrote:
    > 2009/10/14 trans <>
    >
    >> > > from C:/installs/ruby191p243p2/bin/irb:12:in `<main>'

    >>

    > if you want to get one or two first elements from Enumerator (or other
    > Enumerables), you can call
    > Enumerable#take
    >
    > "ab\r\nc".lines.take(1) => ["ab\r\n"]
    > "ab\r\nc".lines.take(2) => ["ab\r\n", "c"]


    ...or

    results = "ab\ncde\nfgh".lines.first(2)

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Oct 14, 2009
    #5
  6. Roger Pack

    7stud -- Guest

    Roger Pack wrote:
    > Currently
    >
    >>> "ab\r\nc".lines[0]

    >


    By the way, I don't think there is ever an occasion to write "\r\n" in
    ruby. As far as I know, you are going to end up with either:

    1) "\r\n" - Unix, Mac
    2) "\r\r\n" - Windows

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Oct 14, 2009
    #6
  7. Roger Pack

    7stud -- Guest

    7stud -- wrote:
    > Roger Pack wrote:
    >> Currently
    >>
    >>>> "ab\r\nc".lines[0]

    >>

    >
    > By the way, I don't think there is ever an occasion to write "\r\n" in
    > ruby. As far as I know, you are going to end up with either:
    >
    > 1) "\r\n" - Unix, Mac
    > 2) "\r\r\n" - Windows


    ..neither of which is correct.
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Oct 14, 2009
    #7
  8. On Tue, Oct 13, 2009 at 2:49 PM, Roger Pack <> wrote=
    :
    > Currently
    >
    >>> "ab\r\nc".lines[0]

    > NoMethodError: undefined method `[]' for #<Enumerator:0x155f220>
    > =C2=A0 =C2=A0 =C2=A0 =C2=A0from (irb):1
    > =C2=A0 =C2=A0 =C2=A0 =C2=A0from C:/installs/ruby191p243p2/bin/irb:12:in `=

    <main>'
    >
    > Doesn't it seem reasonable for this to exist?


    Enumerators may not be rewindable, so [], which would presumably
    provide random access to the values returned by the Enumerator may not
    be all that reasonable. On the other hand, any Enumerator for which
    rewind is usable also could support [], though any general
    implementation using rewind and each would be fairly inefficient.
     
    Christopher Dicely, Oct 14, 2009
    #8
  9. Roger Pack wrote:
    > Currently
    >
    >>> "ab\r\nc".lines[0]

    > NoMethodError: undefined method `[]' for #<Enumerator:0x155f220>
    > from (irb):1
    > from C:/installs/ruby191p243p2/bin/irb:12:in `<main>'
    >
    > Doesn't it seem reasonable for this to exist?
    > Thanks.
    > -r




    What you might need is stringscanner
    It will advanced the scan one call at a time.

    http://www.ruby-doc.org/stdlib/libdoc/strscan/rdoc/classes/StringScanner.html

    irb(main):035:0> s = "one\ntwo\r\nthree\r\nfour\nfive"
    => "one\ntwo\r\nthree\r\nfour\nfive"

    irb(main):036:0> word = StringScanner.new s
    => #<StringScanner 0/25 @ "one\nt...">

    irb(main):037:0> word.scan( /\w+\s+/ )
    => "one\n"
    irb(main):038:0> word.scan( /\w+\s+/ )
    => "two\r\n"
    irb(main):039:0> word.scan( /\w+\s+/ )
    => "three\r\n"
    irb(main):040:0> word.scan( /\w+\s+/ )
    => "four\n"
    irb(main):041:0> word.scan( /\w+\s+/ )
    => nil

    irb(main):042:0> word = StringScanner.new s
    => #<StringScanner 0/25 @ "one\nt...">
    irb(main):043:0> word.scan( /\w+\s+/ ).chomp
    => "one"
    irb(main):044:0> word.scan( /\w+\s+/ ).chomp
    => "two"
    irb(main):045:0> word.scan( /\w+\s+/ ).chomp
    => "three"
    irb(main):046:0> word.scan( /\w+\s+/ ).chomp
    => "four"
    irb(main):047:0> word.scan( /\w+\s+/ ).chomp
    NoMethodError: private method `chomp' called for nil:NilClass
    from (irb):47
    from :0

    chomp doesn't line being passed nil, test for it before calling =)


    you can use reg-ex to split the string into an array and then walk it.

    irb(main):013:0> s = "one\ntwo\r\nthree\r\nfour\nfive"
    => "one\ntwo\n\rthree\r\nfour\rfive"

    irb(main):014:0> s.split( /\s+/ ).each { |x| puts x }
    one
    two
    three
    four
    five
    => ["one", "two", "three", "four", "five"]


    --
    Kind Regards,
    Rajinder Yadav

    http://DevMentor.org
    Do Good ~ Share Freely
     
    Rajinder Yadav, Oct 14, 2009
    #9
    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:
    395
    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:
    452
  3. Thomas Hawtin
    Replies:
    7
    Views:
    393
    Roedy Green
    May 20, 2006
  4. 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:
    132
    Alan Green
    Oct 29, 2003
  5. Kristof Bastiaensen

    RCR: More enumerator functionality

    Kristof Bastiaensen, May 17, 2004, in forum: Ruby
    Replies:
    10
    Views:
    306
    Kristof Bastiaensen
    May 19, 2004
Loading...

Share This Page