Skip to content

Commit

Permalink
Another fix for the Binding.eval! - encode/decode base64 since appare…
Browse files Browse the repository at this point in the history
…ntly there are many chars ruby doesn't like to see in a string that marshalling produces
  • Loading branch information
gtmax committed Jun 20, 2014
1 parent 4ab9c18 commit a5765a6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/serializable_proc/binding.rb
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions lib/serializable_proc/marshalable.rb
@@ -1,3 +1,5 @@
require "base64"

class SerializableProc
module Marshalable

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions spec/proc_like/marshalling_spec.rb
Expand Up @@ -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'
Expand Down

1 comment on commit a5765a6

@ngty
Copy link
Owner

@ngty ngty commented on a5765a6 Jun 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good one, definitely much more robust 👍

Please sign in to comment.