UnMarshal without source file

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

Thanx

···

--
MailTo: anforagimailpuntocom²

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.

···

--
Posted via http://www.ruby-forum.com/\.

Il Sun, 25 Oct 2009 16:28:50 +0100, Brian Candler <b.candler@pobox.com> 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

···

--
Using Opera's revolutionary e-mail client: Opera Web Browser | Faster, Safer, Smarter | Opera

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.

···

--
Posted via http://www.ruby-forum.com/\.