2009/10/14 trans <transfire / gmail.com>

>
>
> On Oct 13, 6:03 pm, Patrick Okui <po... / psg.com> 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"]