Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # encoding: UTF-8
- # info: compile ruby code in file.rb into bytecode and save it compressed as file.rbo
- # or load bytecode from compressed file.rbo and run it when an .rbo file is the input
- # usage: script file.rb
- # script file.rbo
- # requirements: ruby version >= 1.9
- require 'zlib'
- # check ruby version
- (puts "error: minimum ruby version of 1.9"; exit 1) if RUBY_VERSION < "1.9.0"
- # check file argument
- (puts "error: file argument missing (ruby source file.rb or bytecode compiled file.rbo)"; exit 1) if ARGV.size < 1
- # get file name
- myfile = ARGV[0]
- # file exists?
- (puts "error: file #{myfile} does not exist"; exit 1) if not File.exist? myfile
- # get file extension
- myext = myfile.scan(/\.[^\.]*$/)[0]
- # file is bytecode or plain source?
- if myext == ".rb"
- code = RubyVM::InstructionSequence.compile_file(myfile).to_a
- File.write("#{myfile}o", Zlib::Deflate.deflate(Marshal.dump(code)))
- elsif myext == ".rbo"
- code = Marshal.restore((Zlib::Inflate.inflate(File.read(myfile))))
- RubyVM::InstructionSequence.load(code).eval
- else
- puts "error: file type must be a ruby source file.rb or a ruby compiled bytecode file.rbo"
- exit 1
- end
Advertisement
Add Comment
Please, Sign In to add comment