title: Ruby - How to test for nil or empty string from console(stdin)
body: <p>I'm very new to ruby so please bear with me... In a text on Ruby there is a code example that does this:</p>
  
  <pre><code>str = gets
  exit if str.nil? || str.empty?
  str.chomp!
  temp, scale = str.split(" ")
  </code></pre>
  
  <p>My queries are as follows:</p>
  
  <p>Given that <code>gets</code> will only return up to and including the <code>cr</code> why test for the empty string?</p>
  
  <p>If you test the following:</p>
  
  <pre><code>puts nil.to_s.empty?
  puts "".to_s.empty?
  puts "".length            #the empty string      : equates to 0
  puts nil.to_s.length      #the 'to string' method: equates to 0
  </code></pre>
  
  <p>Both will evaluate to true and have zero length. However, the only thing in the stream, if there is only a <code>cr</code>, is the <strong><em>carriage return</em></strong> itself. <code>str</code> will have a length of 1 in the following if you just hit the enter key.</p>
  
  <pre><code>print "enter a string or hit the enter key : "
  str = gets
  puts str.length
  puts str
  </code></pre>
  
  <p>Furthermore, <code>nil</code> in ruby is an <strong><em>object</em></strong>. How the heck am I suppose to capture <strong>that</strong> from <code>stdin</code>?</p>
  
  <p>I now see that the <code>chomp!</code> is misplaced within the text, the author's mistake <strong>not</strong> mine:</p>
  
  <pre><code>str = gets
  str.chomp!
  exit if str.nil? || str.empty? #test for zero length string will now work
  temp, scale = str.split(" ")
  </code></pre>
  
  <p>Granted, I come from java and <em>some</em> common lisp and therefore may be too antiquated to grok this, but I still don't see how the test for <code>nil</code> is appropriate in this context. Maybe there are some conceptual differences in streams that are further along than my reading. Thanks in advance.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>To clear up some confusion here is the code re-written with only the change to placement of the <code>chomp!</code> statement:</p>
  
  <pre><code>#temperature-converter.rb
  #code changed by poster, otherwise as written by author
  print "Please enter a temperature and scale (C or F) : "
  STDOUT.flush   #self explanatory....
  str = gets
  str.chomp!     #new placement of method call -- edit by poster
  exit if str.nil? || str.empty?
  #chomp! original position -- by author
  temp, scale = str.split(" ")
  abort "#{temp} is not a valid number." if temp !~ /-?\d+/
  temp = temp.to_f
  case scale
      when "C", "c"
          f = 1.8 * temp + 32
      when "F", "f"
          c = (5.0/9.0) * (temp - 32)
  else
  abort "Must specify C or F."
  end
  if f.nil?
      puts "#{c} degrees C"
  else
      puts "#{f} degrees F"
  end
  </code></pre>
  
  <p>And the output:</p>
  
  <pre><code>=&gt;ruby temperature-converter.rb
  Please enter a temperature and scale (C or F) : 30 c
  86.0 degrees F
  =&gt;ruby temperature-converter.rb
  Please enter a temperature and scale (C or F) : j c
  j is not a valid number.
  =&gt;ruby temperature-converter.rb
  Please enter a temperature and scale (C or F) : 30 p
  Must specify C or F.
  =&gt;ruby temperature-converter.rb
  Please enter a temperature and scale (C or F) :    #just the enter key
  =&gt;                                                 #did this just exit?!
  </code></pre>
  
  <p>but the answer selected by me is correct, if you use Ctl+D(U) or Ctl+Z(W)
  eof can be emulated and a <code>in '&lt;main&gt;': undefined method 'chomp!' for nil:NilClass (NoMethodError)</code> is error is thrown. Still, the string is never empty so checking the condition doesn't make sense to me.</p>