title: Run a Ruby library from the command-line
body: <p>I've just learned the basics of Ruby after being very happy with Python for several years (I'm still using Python for some things), but I'd like to know if there's an idiom or hack to solve this particular problem.</p>
  
  <p>I have a Ruby script which I'd like to be able to do <code>require script_name</code> with, but I'd also like to be able to run <code>ruby script_name.rb</code> from the terminal and have it run as a command line script. In Python this would be done by having the following structure at the bottom of the script:</p>
  
  <pre><code>if __name__ == '__main__':
      # do something here
  </code></pre>
  
  <p>However, I can't seem to find an equivalent in Ruby. Is there a way of detecting whether or not the current script is being run from the command-line? Maybe some <code>Kernel::</code> method or something? Ideally what I'd like is something like this at the bottom of the script:</p>
  
  <pre><code>if from_command_line?
    # do something here
  end
  </code></pre>