title: Ruby: Setting a global variable by name
body: <p>I am trying to dynamically set (not create, it already has to exist) a global ruby variable in a method. The variable name is determined from the passed symbol. What I am currently doing is the following:</p>
  
  <pre><code>def baz(symbol)
    eval("$#{symbol}_bar = 42")
  end
  
  $foo_bar = 0
  baz(:foo)
  puts $foo_bar # =&gt; 42  
  </code></pre>
  
  <p>But to me, this kind of feels very wrong. Is this the way to do this? Or can it be done differently? Also, I don't know how evals perform in ruby. Does it run much slower than</p>
  
  <pre><code>$foo_bar = 42
  </code></pre>