[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Setting "variable" global variable ?

Johan Holmberg

9/24/2003 1:30:00 PM

5 Answers

Dan Doel

9/24/2003 4:38:00 PM

0

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

Johan Holmberg wrote:

>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
>
>
>
>
>


messju mohr

9/24/2003 4:49:00 PM

0

On Thu, Sep 25, 2003 at 01:37:59AM +0900, Dan Doel wrote:
> 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 Holmberg wrote:
>
> >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
> >
> >
> >
> >
> >

Johan Holmberg

9/24/2003 5:45:00 PM

0

Hal E. Fulton

9/24/2003 5:59:00 PM

0

Johan Holmberg wrote:
> 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


aero6dof

9/25/2003 3:43:00 PM

0

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

$myapp[name] = val

- alan


Johan Holmberg <holmberg@iar.se> wrote in message news:<Pine.GSO.4.50.0309241514380.29659-100000@pjakkur.iar.se>...
> 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]