Restoring global variables?

B

Bertram Scharpf

Hi,

do I have any possibiliy to save and restore global
and instance variables? I think of something like:

user@host$ cat l.rb
class C ; @@i = 0 ; end
$g = ""

cache_globals { load 'm.rb' }
assert $g == "" and C.instance_eval { @@i.zero? }

user@host$ cat m.rb
C.instance_eval { @@i += 99 }
$g << "foo"
user@host$

There are several workarounds, for example starting another
process. What is the smartest way to do it?

Thanks in advance.

Bertram
 
R

Robert Klemme

Bertram Scharpf said:
Hi,

do I have any possibiliy to save and restore global
and instance variables? I think of something like:

user@host$ cat l.rb
class C ; @@i = 0 ; end
$g = ""

cache_globals { load 'm.rb' }
assert $g == "" and C.instance_eval { @@i.zero? }

user@host$ cat m.rb
C.instance_eval { @@i += 99 }
$g << "foo"
user@host$

There are several workarounds, for example starting another
process. What is the smartest way to do it?

This is one way that's better than another process.

module Kernel
private
def cache_globals
store = {}
global_variables.each {|v| store[v] = eval(v)}
begin
return yield
ensure
store.each do |n,v|
begin
eval "#{n}=v"
rescue NameError, SyntaxError, ArgumentError
# ignore
end
end
end
end
end

$f = 0
p $f
cg { $f = 100; p $f }
p $f

You can also create something with #trace_var:
http://www.ruby-doc.org/core/classes/Kernel.html#M001741

Kind regards

robert
 
B

Bertram Scharpf

Hi,

Am Montag, 31. Jan 2005, 08:10:47 +0900 schrieb Robert Klemme:
Bertram Scharpf said:
do I have any possibiliy to save and restore global
and instance variables? I think of something like:

user@host$ cat l.rb
class C ; @@i = 0 ; end
$g = ""

cache_globals { load 'm.rb' }
assert $g == "" and C.instance_eval { @@i.zero? }

user@host$ cat m.rb
C.instance_eval { @@i += 99 }
$g << "foo"
user@host$

There are several workarounds, for example starting another
process. What is the smartest way to do it?

This is one way that's better than another process.

module Kernel
private
def cache_globals
store = {}
global_variables.each {|v| store[v] = eval(v)}
begin
return yield
ensure
store.each do |n,v|
begin
eval "#{n}=v"
rescue NameError, SyntaxError, ArgumentError
# ignore
end
end
end
end
end

$f = 0
p $f
cg { $f = 100; p $f }
p $f

Another nice workaround. Two caveats:

* Newly created variables aren't destroyed.
* side effects:

irb(main):002:0> eq=eval '$='
=> false
irb(main):003:0> eval '$= = eq'
(eval):1: warning: modifying $= is deprecated
=> false
You can also create something with #trace_var:
http://www.ruby-doc.org/core/classes/Kernel.html#M001741

Instantiating about 50 trap routines? No. Besides that, the
above solution could be bypassed using a trace. It's a
security question I asked.

Thanks very much anyway, so far.

Bertram
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,903
Members
47,444
Latest member
Michaeltoyler01

Latest Threads

Top