Module: Marshal

Defined in:
lib/utilrb/marshal/load_with_missing_constants.rb

Defined Under Namespace

Classes: BlackHole

Class Method Summary collapse

Class Method Details

.load_with_missing_constants(str_or_io) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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