rdbundle.rb

#
# rdbundle.rb
# usage: ruby rdbundle.rb ruby-script
# 
# 複数ファイルをRDとそれ以外に分けるフィルタ
# 

require 'optparse'
require 'tempfile'

option = {}
op = OptionParser.new
op.on("-r", "--rd-only", "output only bundled RD part"){ option[:rd_only] = true }


ARGV.empty? and raise "no file direction"

begin
  temp = Tempfile.open("_rdbundle")
  
  op.parse!(ARGV)
  
  puts '=begin'
  ARGF.each do |line|
    if /\A=begin\s/ === line
      while line = ARGF.gets
        break if /\A=end\s/ === line
        print line
      end
    else
      temp.print line unless option[:rd_only]
    end
  end
  puts '=end'
  
  unless option[:rd_only]
    temp.rewind
    temp.each{|line| print line}
  end
ensure
  temp.close if temp
end