title: Accepting Command-Line Arguments into a Ruby Script
body: <p>I'm trying to use the following code to accept in a file as an argument in Terminal which will then be read and update the <code>body</code> variable with its contents. If the file is not passed in then I want to have the prompt where the user can enter their own body copy.</p>
  
  <pre><code>
  require 'posterous'
  
  Posterous.config = {
    'username'  => 'name',
    'password'  => 'pass',
    'api_token' => 'token'
  }
  
  include Posterous
  @site = Site.primary
  
  #GETS POST TITLE
  puts "Post title: "
  title = STDIN.gets.chomp()
  
  if defined?(ARGV)
    filename = ARGV.first
  end
  
  if (defined?(filename))
    body = File.open(filename)
    body = body.read()
  else
    puts "Post body: "
    body = STDIN.gets.chomp()
  end
  puts body
  </code></pre>
  
  <p>When I run the program without passing in a file I get this returned:</p>
  
  <pre><code>
  Post title: 
  Hello
  posterous.rb:21:in `initialize': can't convert nil into String (TypeError)
      from posterous.rb:21:in `open'
      from posterous.rb:21:in `'
  </code></pre>
  
  <p>I'm rather new to ruby and thus am not the best at it. I've tried swapping a lot of things around and changing things but to no avail. What am I doing wrong?</p>