Skip to content

Instantly share code, notes, and snippets.

@headius
Created November 19, 2011 08:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save headius/1378616 to your computer and use it in GitHub Desktop.
Save headius/1378616 to your computer and use it in GitHub Desktop.
require 'jruby'
module Serialize
def self.serialize(obj)
baos = java.io.ByteArrayOutputStream.new
oos = java.io.ObjectOutputStream.new(baos)
oos.write_object(JRuby.reference(obj))
oos.close
String.from_java_bytes(baos.to_byte_array)
end
def self.deserialize(str)
bais = java.io.ByteArrayInputStream.new(str.to_java_bytes)
ois = java.io.ObjectInputStream.new(bais)
ois.read_object
end
end
class Foo
def make_proc
proc { puts 'hello' }
end
end
prc = Foo.new.make_proc
class Proc
def _dump(depth)
Serialize.serialize(self)
end
def self._load(str)
Serialize.deserialize(str)
end
end
dumped = Marshal.dump(prc)
p dumped
prc = Marshal.load(dumped)
prc.call
system ~/projects/jruby $ jruby -X-C proc_marshal.rb
"\004\bu:\tProc\002\227\n\254\355\000\005sr\000\022org.jruby.RubyProcjt\255\035\000\ty\203\002\000\005I\000\004lineL\000\005blockt\000\031Lorg/jruby/runtime/Block;L\000\004filet\000\022Ljava/lang/String;L\000\016sourcePositiont\000&Lorg/jruby/lexer/yacc/ISourcePosition;L\000\004typet\000\036Lorg/jruby/runtime/Block$Type;xr\000\024org.jruby.RubyObject\021bo\361o\020/\315\003\000\000xr\000\031org.jruby.RubyBasicObject%\201\226\325y\332\357\213\003\000\002I\000\005flags[\000\bvarTablet\000\023[Ljava/lang/Object;xp\000\000\000\000ur\000\023[Ljava.lang.Object;\220\316X\237\020s)l\002\000\000xp\000\000\000\000w\006\000\004Procxw\004\000\000\000\000x\000\000\000\027sr\000\027org.jruby.runtime.Block\004\302l\254\320w\307d\002\000\005L\000\abindingt\000\eLorg/jruby/runtime/Binding;L\000\004bodyt\000\035Lorg/jruby/runtime/BlockBody;[\000\aescapedt\000\002[ZL\000\004proct\000\024Lorg/jruby/RubyProc;L\000\004typeq\000~\000\004xpsr\000\031org.jruby.runtime.Binding\316\374\212\v\215\362\202\217\002\000\aI\000\004lineL\000\ndummyScopet\000 Lorg/jruby/runtime/DynamicScope;L\000\fdynamicScopeq\000~\000\022L\000\004fileq\000~\000\002L\000\005framet\000\031Lorg/jruby/runtime/Frame;L\000\004selft\000'Lorg/jruby/runtime/builtin/IRubyObject;L\000\nvisibilityt\000\036Lorg/jruby/runtime/Visibility;xp\000\000\000\027psr\000*org.jruby.runtime.scope.NoVarsDynamicScopeP\2630\204\263d\274\327\002\000\000xr\000\036org.jruby.runtime.DynamicScope\210\036\r\231\254\213\332C\002\000\005L\000\abackrefq\000~\000\024L\000\tevalScopeq\000~\000\022L\000\blastlineq\000~\000\024L\000\006parentq\000~\000\022L\000\vstaticScopet\000\036Lorg/jruby/parser/StaticScope;xpppppsr\000!org.jruby.parser.LocalStaticScope\036\226h\301\271!\305\354\f\000\000xr\000\034org.jruby.parser.StaticScopeC8\332Rx\336\020u\f\000\000xpsq\000~\000\esq\000~\000\eppw%\000\000\000\002\000\003prc\000\006dumped\000\000\000\002\000\000\000\000\000\000\000\000\000\000\377\377\377\377\001\001xq\000~\000\037w\026\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\377\377\377\001\001xq\000~\000\037w\026\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\377\377\377\001\001xt\000\017proc_marshal.rbsr\000\027org.jruby.runtime.Frame>_l2V\216\004x\002\000\bZ\000\016isBindingFrameI\000\njumpTargetI\000\004lineL\000\005blockq\000~\000\001L\000\bfileNameq\000~\000\002L\000\004nameq\000~\000\002L\000\004selfq\000~\000\024L\000\nvisibilityq\000~\000\025xp\000\000\000\003\365\000\000\000\esq\000~\000\vpsr\000\037org.jruby.runtime.NullBlockBody=;\301j\343\247dH\002\000\000xr\000\eorg.jruby.runtime.BlockBody\221\251\265\e\276\031\311\245\002\000\001I\000\fargumentTypexp\000\000\000\000ur\000\002[ZW\217 9\024\270]\342\002\000\000xp\000\000\000\001\000p~r\000\034org.jruby.runtime.Block$Type\000\000\000\000\000\000\000\000\022\000\000xr\000\016java.lang.Enum\000\000\000\000\000\000\000\000\022\000\000xpt\000\006NORMALq\000~\000 t\000\tmake_procsq\000~\000\005\000\000\000\000q\000~\000\nw\005\000\003Fooxw\004\000\000\000\000x~r\000\034org.jruby.runtime.Visibility\000\000\000\000\000\000\000\000\022\000\000xq\000~\000*t\000\006PUBLICq\000~\000.q\000~\0000sr\000\"org.jruby.runtime.InterpretedBlock\242\350\350\026\255\031\e\246\002\000\005Z\000\nnoargblockL\000\005arityt\000\031Lorg/jruby/runtime/Arity;L\000\bassignert\000%Lorg/jruby/runtime/assigner/Assigner;L\000\bbodyNodet\000\024Lorg/jruby/ast/Node;L\000\005scopeq\000~\000\031xq\000~\000%\000\000\000\000\001sr\000\027org.jruby.runtime.Arity\000\000\000\000\000\000\000\001\002\000\001I\000\005valuexp\377\377\377\377sr\0001org.jruby.runtime.assigner.Pre0Rest0Post0Assigner\177h\334\330\201\376\347\t\002\000\000xr\000#org.jruby.runtime.assigner.Assigner\235\331\202\026\311r\276N\002\000\000xpsr\000\031org.jruby.ast.NewlineNode\000\000\000\000\000\000\000\000\f\000\000xr\000\022org.jruby.ast.Node\000\000\000\000\000\000\000\000\f\000\000xpsr\000)org.jruby.lexer.yacc.SimpleSourcePosition\000\000\000\000\000\000\000\000\f\000\000xpw\025\000\017proc_marshal.rb\000\000\000\027xsr\000\035org.jruby.ast.FCallOneArgNode\000\000\000\000\000\000\000\000\f\000\000xr\000\027org.jruby.ast.FCallNode\000\000\000\000\000\000\000\000\f\000\000xq\000~\000=q\000~\000@sr\000\027org.jruby.ast.ArrayNode\000\000\000\000\000\000\000\000\f\000\000xr\000\026org.jruby.ast.ListNode\000\000\000\000\000\000\000\000\f\000\000xq\000~\000=q\000~\000@w\004\000\000\000\001sr\000\025org.jruby.ast.StrNode\000\000\000\000\000\000\000\000\f\000\000xq\000~\000=q\000~\000@ur\000\002[B\254\363\027\370\006\bT\340\002\000\000xp\000\000\000\005helloxw\001\001xpw\006\000\004putsq\000~\000Hxxsr\000!org.jruby.parser.BlockStaticScope\312 $\bau\rk\f\000\000xq\000~\000\034q\000~\000\035q\000~\000\037w\026\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\377\377\377\000\000xuq\000~\000'\000\000\000\001\001q\000~\000\b~q\000~\000)t\000\006LAMBDAq\000~\000 pq\000~\000N"
hello
@ryanstout
Copy link

Just curious if this still works, I get the following: NativeException: java.io.NotSerializableException: org.jruby.runtime.Block at line 8.

Thanks

@kylekyle
Copy link

I've tried a few different versions of JRuby from around 2011, but they all produce the same error: "IOError: org.jruby.runtime.Block". Would you happen to remember what version of JRuby this worked against?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment