From: Dan Doel Date: 2003-09-25T01:37:59+09:00 Subject: Re: Setting "variable" global variable ? 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 > > > > >