[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Restore Kernel.binding to a previous state

Hans Sjunnesson

2/22/2007 3:13:00 PM

Hey all,

I'm implementing a simple queue system with which I'm evaling 'jobs',
like so:

code = lambda {eval(job)}
code.call

Where 'job' is a string containing ruby code.
The problem I'm having is that I need these jobs to evaluate in a
clean environment, uncluttered by the previous jobs. What I'd like is
something along the lines of this:

temp = Kernel.binding
code = lambda {eval(job)}
code.call
Kernel.binding = temp

Though that looks sort of dangerous.
Another idea might be to be able to create a binding which is a deep
copy of the current binding. I'm not sure if this has been done before.

8 Answers

Ara.T.Howard

2/22/2007 3:39:00 PM

0

Ben Bleything

2/22/2007 4:01:00 PM

0

On Fri, Feb 23, 2007, Hans Sjunnesson wrote:
> Where 'job' is a string containing ruby code.
> The problem I'm having is that I need these jobs to evaluate in a
> clean environment, uncluttered by the previous jobs. What I'd like is
> something along the lines of this:
>
> temp = Kernel.binding
> code = lambda {eval(job)}
> code.call
> Kernel.binding = temp

I have (what I believe to be) a similar problem in some code I'm working
on. It allows plugins written by the user to be executed, but each time
it needs a clean environment. I'm taking advantage of the fact that you
can pass eval a binding in which to execute. I have a simple binding
factory which just creates new instances of itself and returns their
binding:

class BindingFactory
def self::get_binding
return self.new.send( :binding )
end
end

eval( action, BindingFactory.get_binding )

For my purposes, it works like a charm:

>> ex1 = "a = :sym"
=> "a = :sym"
>> ex2 = "puts a"
=> "puts a"
>> eval ex1, BindingFactory.get_binding
=> :sym
>> eval ex2, BindingFactory.get_binding
NameError: undefined local variable or method `a' for #<BindingFactory:0x406f60f4>
from (irb):18:in `send'
from (irb):18:in `get_binding'
from (irb):24
from :0

Is that helpful?

Ben

Hans Sjunnesson

2/22/2007 7:37:00 PM

0

On Feb 22, 5:01 pm, Ben Bleything <b...@bleything.net> wrote:
> On Fri, Feb 23, 2007, Hans Sjunnesson wrote:
> > Where 'job' is a string containing ruby code.
> > The problem I'm having is that I need these jobs to evaluate in a
> > clean environment, uncluttered by the previous jobs. What I'd like is
> > something along the lines of this:
>
> > temp = Kernel.binding
> > code = lambda {eval(job)}
> > code.call
> > Kernel.binding = temp
>
> I have (what I believe to be) a similar problem in some code I'm working
> on. It allows plugins written by the user to be executed, but each time
> it needs a clean environment. I'm taking advantage of the fact that you
> can pass eval a binding in which to execute. I have a simple binding
> factory which just creates new instances of itself and returns their
> binding:
>
> class BindingFactory
> def self::get_binding
> return self.new.send( :binding )
> end
> end
>
> eval( action, BindingFactory.get_binding )
>
> For my purposes, it works like a charm:
>
> >> ex1 = "a = :sym"
> => "a = :sym"
> >> ex2 = "puts a"
> => "puts a"
> >> eval ex1, BindingFactory.get_binding
> => :sym
> >> eval ex2, BindingFactory.get_binding
>
> NameError: undefined local variable or method `a' for #<BindingFactory:0x406f60f4>
> from (irb):18:in `send'
> from (irb):18:in `get_binding'
> from (irb):24
> from :0
>
> Is that helpful?
>
> Ben

Helpful - yes, in finding out that using eval might not be what I'm
after.
I thought, at first, that a binding housed the entirety of a running
ruby process' context.

I'm wanting to run ruby snippets like the below:

require 'rake'
default = task :default do
# do stuff
end
default.invoke

But for my problem, I might be better off using Kernel::system, and
invoking ruby in an external process, even though I'd really like my
little code-snippet to be able to access, for instance, the Logger
I've set up in my queue-system.
I might get away with that by modifying your BindingFactory to pass
objects into the created binding as local variables. But stuff like
rake modify the running context in more ways than setting local
variables.

Ara.T.Howard

2/22/2007 7:56:00 PM

0

Ben Bleything

2/22/2007 8:12:00 PM

0

On Fri, Feb 23, 2007, Hans Sjunnesson wrote:
> Helpful - yes, in finding out that using eval might not be what I'm
> after. I thought, at first, that a binding housed the entirety of a
> running ruby process' context.

Sort of. It includes the context of wherever the binding was created.
If you call binding in the top level of your script, you'll get all of
that context that you can execute inside of.

> I'm wanting to run ruby snippets like the below:
>
> require 'rake'
> default = task :default do
> # do stuff
> end
> default.invoke

That's exactly what I'm doing :)

> But for my problem, I might be better off using Kernel::system, and
> invoking ruby in an external process, even though I'd really like my
> little code-snippet to be able to access, for instance, the Logger
> I've set up in my queue-system.

You can pretty easily inject variables into your binding. As I looked
at my code, I realized that I actually am not using the BindingFactory
idea (it was a prototype that I ended up not sticking with).

Instead, I have a Workspace class. It's got a method called #set_value
that takes a name and value and sets an instance variable and adds an
attr_reader inside the workspace's eigenclass.

Then, the tasks that I've got (I call them Command in my code) know how
to execute themselves inside a workspace, so I can just say

command.execute( workspace )

and it calls the proc inside that binding.

I'm working on getting my code released (gotta get the company to
agree), and until then I can't show too much, but I'm happy to talk
about it more if it sounds similar enough to what you're doing to help.

Ben

Hans Sjunnesson

2/22/2007 9:35:00 PM

0

On Feb 22, 8:56 pm, ara.t.how...@noaa.gov wrote:
> On Fri, 23 Feb 2007, Hans Sjunnesson wrote:
> >> Ben
>
> > Helpful - yes, in finding out that using eval might not be what I'm
> > after.
> > I thought, at first, that a binding housed the entirety of a running
> > ruby process' context.
>
> > I'm wanting to run ruby snippets like the below:
>
> > require 'rake'
> > default = task :default do
> > # do stuff
> > end
> > default.invoke
>
> > But for my problem, I might be better off using Kernel::system, and
> > invoking ruby in an external process, even though I'd really like my
> > little code-snippet to be able to access, for instance, the Logger
> > I've set up in my queue-system.
> > I might get away with that by modifying your BindingFactory to pass
> > objects into the created binding as local variables. But stuff like
> > rake modify the running context in more ways than setting local
> > variables.
>
> why don't you fork?
>
> -a
> --
> be kind whenever possible... it is always possible.
> - the dalai lama

Actually, that works perfectly. I was under the impression that
Kernel.fork wrapped native fork() and thus wasn't portable. But it
works well for what I need.


Ara.T.Howard

2/22/2007 10:09:00 PM

0

Erik Veenstra

2/24/2007 9:20:00 AM

0

> The problem I'm having is that I need these jobs to evaluate
> in a clean environment, uncluttered by the previous jobs.

You could use an anonymous module to de_inspect a string:

a = "[1, 2, 3, 4]".de_inspect

gegroet,
Erik V. - http://www.erikve...

----------------------------------------------------------------

class String
def de_inspect
Thread.new do
$SAFE = 4

eval(self, Module.new.module_eval{binding})
end.value
end
end

class String
def de_inspect_unsafe
eval(self, Module.new.module_eval{binding})
end
end

----------------------------------------------------------------