Robert Klemme
4/17/2006 9:34:00 AM
jonT wrote:
> Hi,
>
> Here's a tricky one:
>
> Goal: I want to see test whether I'm going to have problems bringing
> back some marshaled data in a new Ruby instance, and take necessary
> action if so. So before I save my marshaled data to disk, I'd like to
> attempt loading it and see if I get an Arguement error. Makes sense? :)
>
> I'd hoped that the following would suffice: evaluate the Marshal
> dumping in the current binding, then attempt the load in another. Since
> the executions should be seperate, the second binding shouldn't know
> what went on in the first and I should have the equivilent of dumping
> and loading in two seperate Ruby instances?
>
> Anyway, running the below code results in f being loaded fine :(.
>
> * Any ideas for how I can fix this?
> * Any better / alternative ways of solving the problem?
>
> Cheers,
> jonT
>
>
> def jim
> binding
> end
>
> def bob
> binding
> end
>
> jim_b=jim
> bob_b=bob
>
> puts jim_b
> puts bob_b
>
> eval("class F;end",jim_b)
> eval("f=F.new",jim_b)
> data=eval("Marshal.dump(f)",jim_b)
>
> # i want this to fail with an Argument error
> puts eval("Marshal.load('" + data + "')",bob_b)
That the loading succeeds doesn't necessarily mean that f is defined
properly. You have to access f to check that, i.e. you should do any of
these:
eval "p f", bob_b
p eval("f", bob_b)
Also, I'm not sure that your code will work properly. You may have to
use data.inspect.
Kind regards
robert