title: When using Ruby, is there a way not to use $ for global variables, or structure the program to put the globals as some locals within a main method?
body: <p>That is, possibly structuring it so that it is like an IIFE in JavaScript so that all those variables are visible only within a function scope?</p>
  
  <p>When converting some Go programs into Ruby to compare their running speed, I had to convert all the Go global variables from <code>width</code> to <code>$width</code> in Ruby, so that inside any function, these global values can be used.</p>
  
  <p>But if there are 15 or 20 of these variables, then it becomes very troublesome to convert.  In Ruby, is there a way to not use <code>$</code> for global variables?  I tried to define a <code>main</code> method first, and hoping it is local inside of <code>main</code>:</p>
  
  <pre><code>def main
  
    foo = 1
  
    def another
      p foo
    end
  
    another()
  
  end
  
  main()
  </code></pre>
  
  <p>but it won't work.  Inside of <code>another()</code>, it still won't recognize <code>foo</code> and all the <code>foo</code> have to be changed to <code>$foo</code> for it to work.</p>
  
  <p>As per request, this is some sample of the Go code:</p>
  
  <pre><code>var palette = []color.Color{color.White, color.Black}
  
  const (
      whiteIndex = 0            
      blackIndex = 1            
      width, height = 800, 400
      viewAngle = math.Pi / 9
  )
  </code></pre>
  
  <p>it actually is quite typical for some small Go program (or C program), to have some global variables at the very top, and then the <code>main</code> and some functions down below.  Since the whole program is not very big, there usually is not so much concern for some global variables such as the color, width, height, etc.</p>