Setting "variable" global variable ?

J

Johan Holmberg

Hi !

I wonder if there is any way in Ruby of setting a "variable" global
variable at runtime, without using "eval".

For example, could the following be done without "eval":

def my_set_global_variable(name, val)
eval "#{name} = #{val.inspect}"
end

my_set_global_variable("$foo", [1,2,3])

p $foo # gives [1, 2, 3]


I would like to avoid "serializing" the "val" value above
(done with the "inspect" call), and assign the original "val"
directly instead.

For instance variables there is "instance_variable_set", but is
there something corresponding for global variables ?

/Johan Holmberg
 
D

Dan Doel

def global_variable_set(name, val)
eval("proc { | x | $#{name} = x }").call val
end
global_variable_set("foo", [1, 2, 3])
 
M

messju mohr

def global_variable_set(name, val)
eval("proc { | x | $#{name} = x }").call val
end
global_variable_set("foo", [1, 2, 3])

or simply:

def global_variable_set(name, val)
eval "$#{name} = val"
end
global_variable_set("foo", [1, 2, 3])


but the original question was how to do it *without* the eval.

greetings
messju


Johan said:
Hi !

I wonder if there is any way in Ruby of setting a "variable" global
variable at runtime, without using "eval".

For example, could the following be done without "eval":

def my_set_global_variable(name, val)
eval "#{name} = #{val.inspect}"
end

my_set_global_variable("$foo", [1,2,3])

p $foo # gives [1, 2, 3]


I would like to avoid "serializing" the "val" value above
(done with the "inspect" call), and assign the original "val"
directly instead.

For instance variables there is "instance_variable_set", but is
there something corresponding for global variables ?

/Johan Holmberg
 
J

Johan Holmberg

or simply:

def global_variable_set(name, val)
eval "$#{name} = val"
end
global_variable_set("foo", [1, 2, 3])

but the original question was how to do it *without* the eval.

I think it is quite "ok".
At least there is no extra copying (or serialization) of "val"
as in my original example.

I didn't realize that the eval would "see" the value of "val",
but apparently it does.

But I'm still interested in a solution without "eval".

Thanks for the answers soo far,
/Johan Holmberg
 
H

Hal Fulton

Johan said:
I think it is quite "ok".
At least there is no extra copying (or serialization) of "val"
as in my original example.

This is probably obvious and likely irrelevant, but I will mention
that a caller could do something malicious by passing in an "evil"
string to be evaluated.
I didn't realize that the eval would "see" the value of "val",
but apparently it does.

It sees everything in the current binding. For example,
x = eval("val") is the same as x = val. (There are probably some
strange exceptions to this.)
But I'm still interested in a solution without "eval".

My impression is that without a Ruby builtin, it is not really
possible without eval.

Exceptions might be:

1. Maybe an extension could implement a method to do it.
2. You could do something crazy like write an assignment to
a file and then require the file... but it would fail under
many circumstances.

Hal
 
M

messju mohr

Hi --



I don't want to be responsible for encouraging non-vigilance toward
things like this... but if val is a string, wouldn't eval'ing val
just result in the string, not a further evaluation of the string?

a = "puts 'hi'"
eval "b = a"

b is now "puts 'hi'", but 'hi' doesn't get puts'd.

(Or is there another, more evil scenario I'm not thinking of?)

he meant this line:

eval "$#{name} = val"

and passing sth. evil in "name"

greetings
messju
 
A

Alan Chen

This is probably a stupid question, but wouldn't it be simpler to use a global hash?

$myapp[name] = val

- alan
 

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
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top