From: messju mohr Date: 2003-09-25T01:48:39+09:00 Subject: Re: Setting "variable" global variable ? 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 > > > > > > > > > >