From: kimhyunkang@... Date: 2009-10-14T11:39:40+09:00 Subject: Re: no Enumerator#[] ? --001636c5bb4dcb604a0475dc12dd Content-Type: text/plain; charset=ISO-8859-1 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 # > > > from (irb):1 > > > from C:/installs/ruby191p243p2/bin/irb:12:in `
' > > > > > 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"] --001636c5bb4dcb604a0475dc12dd--