No Enumerator#[]?

Currently

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

Doesn’t it seem reasonable for this to exist?
Thanks.
-r

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

Currently

“ab\r\nc”.lines[0]
NoMethodError: undefined method []' for #<Enumerator:0x155f220> 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].

2009/10/14 trans [email protected]

   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”]

On Oct 13, 6:03 pm, Patrick O. [email protected] wrote:

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… ?

Doesn’t it seem reasonable for this to exist?

Well this is ruby…so add away!

unknown wrote:

2009/10/14 trans [email protected]

   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)

7stud – wrote:

Roger P. 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.

Roger P. 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

On Tue, Oct 13, 2009 at 2:49 PM, Roger P. [email protected]
wrote:

Currently

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

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.

Roger P. wrote:

Currently

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

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 Y.

http://DevMentor.org
Do Good ~ Share Freely

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs