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