lib/rbm.rb in rbm-0.0.3 vs lib/rbm.rb in rbm-1.0.0

- old
+ new

@@ -1,33 +1,36 @@ require "optparse" -require "rbm/benchmarker" -require "rbm/version" module RBM + require "rbm/benchmarker" + require "rbm/fragment" + require "rbm/version" + class << self - def start - fragments, options = parse_options(ARGV) + def start(args) + fragments, options = parse_options(args) if load_paths = options.delete(:load_paths) - $LOAD_PATH.concat(load_paths) + $LOAD_PATH.unshift(load_paths) end begin if requires = options.delete(:requires) requires.each { |r| require(r) } end rescue LoadError => e - puts e - exit + $stderr.puts(e) + exit 1 end Benchmarker.new(fragments, options).run end private def parse_options(args) - fragments, options = [], { :requires => [], :load_paths => [] } + fragments = [] + options = { :requires => [], :load_paths => [] } current_name, current_prerun, current_postrun = nil, nil, nil op = OptionParser.new do |op| op.banner << " [--name name] [--pre code] code [[--name name] [--pre code] code...]" @@ -35,13 +38,10 @@ op.separator "Ruby Options:" op.on("-r", "--require file[,file]", Array, "Files to require before benchmarking") { |a| options[:requires].concat(a) } op.on("-I", "--load-path path[,path]", Array, "Paths to append to $LOAD_PATH") { |a| options[:load_paths].concat(a) } - # op.separator "" - # op.separator "Benchmark Options:" - op.separator "" op.separator "Code Fragment Options:" op.on("-n", "--times n", Integer, "Number of times to run each code fragment") { |i| options[:times] = i } op.on("-i", "--init code", String, "Code to run before every code fragment") { |s| options[:init] = s } @@ -60,18 +60,18 @@ op.on("-h", "--help", "Print this message and exit") do puts op exit end end - op.order!(args) do |fragment| + op.order!(args) do |code| # block gets run for each non-option argument - fragments << { :name => current_name, :prerun => current_prerun, :postrun => current_postrun, :fragment => fragment } + fragments << Fragment.new(code, current_name, current_prerun, current_postrun) current_name, current_prerun, current_postrun = nil, nil, nil end if fragments.empty? puts op - exit + exit 1 end [fragments, options] end end