From a5765a6636bd8ed9aef42047a1fd7f087c46ef50 Mon Sep 17 00:00:00 2001 From: gtmax Date: Wed, 18 Jun 2014 13:09:09 +0300 Subject: [PATCH] Another fix for the Binding.eval! - encode/decode base64 since apparently there are many chars ruby doesn't like to see in a string that marshalling produces --- lib/serializable_proc/binding.rb | 2 +- lib/serializable_proc/marshalable.rb | 6 ++++-- spec/proc_like/marshalling_spec.rb | 6 ++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/serializable_proc/binding.rb b/lib/serializable_proc/binding.rb index 2474dc6..aa0205c 100644 --- a/lib/serializable_proc/binding.rb +++ b/lib/serializable_proc/binding.rb @@ -29,7 +29,7 @@ def eval!(binding = nil) private def declare_vars - @declare_vars ||= @vars.map{|(k,v)| "#{k} = Marshal.load(%q|#{mdump(v)}|)" } * '; ' + @declare_vars ||= @vars.map{|(k,v)| "#{k} = #{evaluable_string(v)}" } * '; ' end def bounded_val(var, binding) diff --git a/lib/serializable_proc/marshalable.rb b/lib/serializable_proc/marshalable.rb index 179ab77..6c8435c 100644 --- a/lib/serializable_proc/marshalable.rb +++ b/lib/serializable_proc/marshalable.rb @@ -1,3 +1,5 @@ +require "base64" + class SerializableProc module Marshalable @@ -37,8 +39,8 @@ def marshal_load(data) protected - def mdump(val) - Marshal.dump(val).gsub('|','\|') + def evaluable_string(val) + "Marshal.load(Base64.strict_decode64(%q|#{Base64.strict_encode64(Marshal.dump(val)).gsub('|','\|')}|))" end def mclone(val) diff --git a/spec/proc_like/marshalling_spec.rb b/spec/proc_like/marshalling_spec.rb index 5704e60..2bf2020 100644 --- a/spec/proc_like/marshalling_spec.rb +++ b/spec/proc_like/marshalling_spec.rb @@ -39,6 +39,12 @@ Marshal.load(Marshal.dump(s_proc)).call.should.equal('#{') end + should 'handle local variables that marshal with some non-friendly char' do + require 'time' + v = Time.parse('Mon, 16 Jun 2014 11:23:13') + s_proc = SerializableProc.new{ v } + Marshal.load(Marshal.dump(s_proc)).call.should.equal(v) + end should 'handle instance variables' do @x, @y, expected = 'awe', 'some', 'awesome'