# UnMarshal without source file

**URL:** <https://www.ruby-forum.com/t/unmarshal-without-source-file/197106>\
**Category:** Ruby\
**Created:** [October 25, 2009, 3:56pm UTC](https://www.ruby-forum.com/t/unmarshal-without-source-file/197106 "2009-10-25T15:56:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manny\_Calavera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@Manny\_Calavera](https://www.ruby-forum.com/u/Manny_Calavera)\
**Post date:** [October 25, 2009, 3:56pm UTC](https://www.ruby-forum.com/t/unmarshal-without-source-file/197106/1 "2009-10-25T15:56:45Z")

</div>

I’m looking for a STANDARD methods to unmarshal a class istance without  
have source file.

Thanx

---

<div class="post-metadata">

**Author:** ![Manny\_Calavera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@Manny\_Calavera](https://www.ruby-forum.com/u/Manny_Calavera)\
**Post date:** [October 25, 2009, 4:28pm UTC](https://www.ruby-forum.com/t/unmarshal-without-source-file/197106/2 "2009-10-25T16:28:48Z")

</div>

Manny Calavera wrote:

> I’m looking for a STANDARD methods to unmarshal a class istance without  
> have source file.

How would you want it to work? For example, if it contains an object of  
class Foo::Bar, and such class does not exist at runtime, would you want  
it to create a fresh Foo::Bar class? Such class would have no methods of  
course, apart from those inherited from Object.

A quick experiment suggests that Object.const\_missing is not invoked by  
Marshal.load, so perhaps parsing the Marshal format yourself is the best  
bet.

---

<div class="post-metadata">

**Author:** ![Manny\_Calavera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@Manny\_Calavera](https://www.ruby-forum.com/u/Manny_Calavera)\
**Post date:** [October 27, 2009, 12:06pm UTC](https://www.ruby-forum.com/t/unmarshal-without-source-file/197106/3 "2009-10-27T12:06:48Z")

</div>

Il Sun, 25 Oct 2009 16:28:50 +0100, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
vergò:

> Manny Calavera wrote:
> 
> > I’m looking for a STANDARD methods to unmarshal a class istance without  
> > have source file.
> 
> How would you want it to work? For example, if it contains an object of  
> class Foo::Bar, and such class does not exist at runtime, would you want  
> it to create a fresh Foo::Bar class? Such class would have no methods of  
> course, apart from those inherited from Object.

My idea was:

class Klass  
def method  
“It work!”  
end  
end

class MyMarshal  
def MyMarshal.dump ist  
s=“class\ndef method\n"It work!"\nend\nend\n”  
s\<\< “§”  
s\<\< Marshal.dump(ist)  
s  
end

def MYMarshal.load s  
array=s.split/§/  
eval array[0]  
Marshal.load array[1]  
end  
end

But something more STANDARD

---

<div class="post-metadata">

**Author:** ![Manny\_Calavera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@Manny\_Calavera](https://www.ruby-forum.com/u/Manny_Calavera)\
**Post date:** [October 27, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/unmarshal-without-source-file/197106/4 "2009-10-27T15:55:59Z")

</div>

Manny Calavera wrote:

> My idea was:  
> …  
> But something more STANDARD

Well, a Class itself is not marshalable, nor is the singleton class of  
an object.

If you want to save and restore the _definitions_ of classes  
automatically, you’ll have to do something very funky using ParseTree or  
the like.

Otherwise, I’d say your approach might be reasonable, but is rather  
dangerous and duplicates definitions of methods. How about this?

module MyMarshal  
def dump(obj)  
Marshal.dump [$LOADED\_FEATURES, Marshal.dump(obj)]  
end  
module\_function :dump

```
def load(str)
  requires, dump = Marshal.load(str)
  requires.each do |r|
    require r.sub(/\.rb\z/,'')
  end
  Marshal.load(dump)
end
module_function :load

```

end

This depends on your class definitions being available in source files  
which are loaded using ‘require’. It has obvious downsides, but  
something like this might work for you.

It really depends on what your application is trying to do. Why are you  
loading objects which are not defined in the context of the loading  
application? How would you know what methods to call on those objects?

Regards,

Brian.
