2010/5/27 Martin Hansen <mail / maasha.dk>:
> I am writing generic command line scripts and here is a minimal example:
> http://pastie.org/979581 - which I am quite happy with. Now the required
> 'biopieces' class is here http://pastie.org/979577 - but this I am not
> really happy with. Mainly, I think it is poorly organized? Suggestions?

I would extract the opion parsing from that class and either use a
Hash, OpenStruct or custom class for transportation of your options.
That way you make your bio pieces processing independent from the
interface (command line, graphical application etc.).

Then I would extend the parsing algorithm and offer a similar
interface like CSV or File along the lines of:

class BioPieces
  include Enumerable

  def self.open(file_name)
    File.open file_name do |io|
      yield new io
    end
  end

  def self.foreach(file_name, &b)
    open file_name do |bp|
      bp.each_record(&b)
    end
  end

  def initialize(io)
    @io = io
  end

  def each_record
    rec = {}

    @io.each_line do |line|
      case line
      when /^([^:])+:\s*(.*)$/
        rec[$1.strip] = $2.strip
      when /^-+$/
        yield rec
        rec = {}
      else
        raise "Invalid input: %p" % line
      end
    end

    # maybe add this:
    # yield rec unless rec.empty?

    self
  end

  alias each each_record
end

Btw, your code looks pretty clean and well documented.  That's good!

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/