title: Why does Ruby pollute the global namespace with Classes and Functions but not variable?
body: <p>Having this piece of code in test1.rb</p>
  
  <pre><code>my_var = 42
  
  def my_func()
    42
  end
  
  class MyCLS
    attr_accessor :prop
    def initialize()
      @prop = 42
    end
  end
  </code></pre>
  
  <p>Then in the interpreter I require it in irb</p>
  
  <pre><code>&gt; require './test1.rb'
  &gt; MyCLS.new().prop
    =&gt; 42
  &gt; my_func()
    =&gt; 42
  &gt; my_var
  NameError: undefined local variable or method `my_var' for main:Object
  </code></pre>
  
  <p>I am confused, ruby seems quite happy to pollute the global namespace with classes and functions, but refuse to do the same with my_var? I imagine this is to avoid name collisions and bugs. But the problem is only partially solved as it is still present with Class and Function. Maybe just a little less prone to happen?</p>
  
  <p>So now imagine this second file test2.rb</p>
  
  <pre><code>def my_func()
    43
  end
  
  class MyCLS
    attr_accessor :prop
    def initialize()
      @prop = 43
    end
  end
  </code></pre>
  
  <p>And then execute it</p>
  
  <pre><code>&gt; require './test1.rb'
  &gt; require './test2.rb'
  &gt; MyCLS.new().prop
    =&gt; 43
  &gt; my_func()
    =&gt; 43
  </code></pre>
  
  <p>Is that normal that the previous globals MyCLS and my_func get silently overwritten? Isn't this highly likely to break a software because a gem decided to add/rename a Class or a function somewhere? All of this seems very brittle and dangerous.</p>
  
  <p>I am aware of modules and I tried them with little success (awkward, and once again they are globals)</p>
  
  <p>Is there ways to prevent this or mitigate what seems like a language design flaw? </p>
  
  <p>Edit: Another example</p>
  
  <pre><code># test1.rb
  def my_func()
    42
  end
  
  # test2.rb
  puts my_func()
  
  # test3.rb
  class Example
    require './test1.rb'
  end
  
  class AnotherExample
    require './test2.rb'
  end
  
  # command line
  $ ruby test3.rb
  42
  </code></pre>