module Marshal

Public Class Methods

load_with_missing_constants(str_or_io) click to toggle source
# File lib/utilrb/marshal/load_with_missing_constants.rb, line 33
def self.load_with_missing_constants(str_or_io)
    if str_or_io.respond_to?(:tell)
        original_pos = str_or_io.tell
    end

    self.load(str_or_io)
rescue Exception => e
    case e.message
    when /undefined class\/module ((?:\w+)(?:::\w+)*)(?:::)?$/
        full_name = $1
        path = $1.split('::')
        *path, klass = *path
        mod = path.inject(Object) { |m, n| m.const_get(n) }

        blackhole = Class.new(BlackHole) do
            @name = full_name
        end
        mod.const_set(klass, blackhole)

        if original_pos
            str_or_io.seek(original_pos)
        end
        retry
    end
    raise
end