title: Ruby NATS simple publishing
body: <p>I'm learning NATS in Ruby and wanted to create a simple "cat" from stdin.  Either I get all the lines and the loop doesn't exit, or nothing gets sent.</p>
  
  <pre><code>!/usr/bin/env ruby
  
  require "nats/client"
  
  queue = ARGV.shift
  
  NATS.start do
    STDIN.each_line do |line|
      puts "will send: #{line}"
      NATS.publish(queue, line)
    end
  
    NATS.stop
  end
  
  puts "... #{__LINE__}"
  exit 0
  </code></pre>
  
  <p>And I run with:</p>
  
  <pre><code># cat cat.rb | ./cat.rb myqueue
  </code></pre>
  
  <p>With NATS.stop enabled, nothing shows up on the queue, though it correctly shows each line that it will attempt to send, and the program exits:</p>
  
  <pre><code># cat cat.rb | ./cat.rb myqueue
  will send: #!/usr/bin/env ruby
  will send: 
  will send: require "nats/client"
  will send: 
  will send: queue = ARGV.shift
  will send: 
  will send: NATS.start do
  will send:   STDIN.each_line do |line|
  will send:     puts "will send: #{line}"
  will send:     NATS.publish(queue, line)
  will send:   end
  will send: 
  will send:   NATS.stop
  will send: end
  will send: 
  will send: puts "... #{__LINE__}"
  will send: exit 0
  will send: 
  ... 16
  #
  </code></pre>
  
  <p>And with NATS.stop commented out, all the lines are sent to the queue, but the program doesn't exit:</p>
  
  <pre><code># cat cat.rb | ./cat.rb myqueue
  will send: #!/usr/bin/env ruby
  will send: 
  will send: require "nats/client"
  will send: 
  will send: queue = ARGV.shift
  will send: 
  will send: NATS.start do
  will send:   STDIN.each_line do |line|
  will send:     puts "will send: #{line}"
  will send:     NATS.publish(queue, line)
  will send:   end
  will send: 
  will send:   # NATS.stop
  will send: end
  will send: 
  will send: puts "... #{__LINE__}"
  will send: exit 0
  will send: 
  &lt;and program sits here&gt;
  </code></pre>
  
  <p>What am I missing?  Isn't there something easier for just sending a message from Ruby that doesn't involve multi-threading issues?</p>