title: Can I write Ruby code that is executed only when my script is run, but not when it is required?
body: <p>I want to write a Ruby script something like this:</p>
  
  <pre><code>class Foo
    # instance methods here
  
    def self.run
      foo = Foo.new
      # do stuff here
    end
  end
  
  # This code should only be executed when run as a script, but not when required into another file
  unless required_in?  # &lt;-- not a real Kernel method
    Foo.run
  end
  # ------------------------------------------------------------------------------------------
  </code></pre>
  
  <p>I want to be able to unit test it, which is why I don't want the code outside of the class to run unless I execute the script directly, i.e. <code>ruby foo_it_up.rb</code>.</p>
  
  <p>I know I can simply put the <code>Foo</code> class in another file and <code>require 'foo'</code> in my script. In fact, that is probably a better way to do it, just in case <code>Foo</code>'s functionality is needed somewhere else. So my question is more academic than anything, but I'd still be interested in knowing how to do this in Ruby.</p>