Re: Final iteration lost?
John W Higgins <[email protected]> Thu, 8 Aug 2019 12:59:48 -0700
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CAPhAwGwZDgs5mSKvmsaAtFyTWiE6+DmZyd7vujaNRywOU+6MvA@mail.gmail.com> |
On Thu, Aug 8, 2019 at 4:07 AM Leam Hall <[email protected]> wrote: > I'm parsing a data file with each_line, each section starts with a line > that ends in ")". Each section has 2-3 subsections, delimited by blank > lines. The original test for a new section was: > > if line.end_with?(')') > > The better option here is to create a "job" and have it process the rows as opposed to processing rows and then creating a job jobs = [] current_job = nil jobs_file.each_line do |line| if line.end_with? ')' current_job = Job.new jobs << current_job end current_job.process_line(line) end class Job def initialize @data = {} end def process_line(line) if @data['header'].nil? process_header(line) elsif @data['blurb'].nil? @data['blurb'] = line elsif @data['tech'].nil? @data['tech'] = line else @data['extra'] = line end end def process_header(line) ..... end end John Unsubscribe: <mailto:[email protected]?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>