Module: Rbyte

Defined in:
lib/rbyte.rb

Defined Under Namespace

Modules: RequirePatch

Class Method Summary collapse

Class Method Details

.compile_file(path) ⇒ Object

Compile a Ruby file to a Ruby byte code (rbc) file. The rbc file will be placed next to the Ruby file. If the method returns false, than compilation failed.



78
79
80
81
82
83
84
85
86
87
# File 'lib/rbyte.rb', line 78

def compile_file(path)
  path = "#{path}.rb" unless path =~ /\.rb\Z/
  res  = RubyVM::InstructionSequence.compile_file(path)
  data = Marshal.dump(res.to_a)
  rbc_path = path + "c"
  File.open(rbc_path, "w+") {|f| f.write data }
rescue NotImplementedError
  # Ruby bug with terminated objects
  false
end

.decompile_file(path) ⇒ Object

Read a rbc file and evaluate it



69
70
71
72
# File 'lib/rbyte.rb', line 69

def decompile_file(path)
  res = Marshal.load(File.read(path))
  RubyVM::InstructionSequence.load_array(res).eval
end

.search_for_rbc_file(path) ⇒ Object

:nodoc:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rbyte.rb', line 50

def search_for_rbc_file(path) #:nodoc:
  return unless supported_file?(path)
  path.gsub!(/\.rbc\Z/, "")
  $LOAD_PATH.each do |root|
    test_path = File.join(root, path)

    # Test for rbc files
    rbc_test_path = test_path + ".rbc"
    return rbc_test_path if File.file?(rbc_test_path)

    # Test for rb files
    rb_test_path = test_path + ".rb"
    return rb_test_path if File.file?(rb_test_path)
  end
  nil
end

.supported_file?(path) ⇒ Boolean

Only paths without a extension or with an extension of .rbc are are supported. If you require a file with a rb extension, it’s assumed you only want plain ruby files.

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/rbyte.rb', line 96

def supported_file?(path)
  ext = File.extname(path)
  return true if ext.empty?
  ext =~ /\.rbc\Z/
end