no Enumerator#[] ?

T

trans

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... ?
 
K

kimhyunkang

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

2009/10/14 trans said:
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"]
 
7

7stud --

Doesn't it seem reasonable for this to exist?

Well this is ruby...so add away!
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)
 
C

Christopher Dicely

Currently
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 `=
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.
 
R

Rajinder Yadav

Roger said:
Currently
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top