Q:

Net::SSH::Multi using the session.exec, how do you get the output straight away? Ruby

So I've been trying to use the Net::SSH::Multi to login to multiple machines using the via SSH, and then executing shell commands on the remote machines with session.exec("some_command").
The Code:
#!/usr/bin/ruby
require 'rubygems'
require 'net/ssh'
require 'net/ssh/multi'

Net::SSH::Multi.start do |session|
        # Connect to remote machines
        ### Change this!!###
        session.use 'user@server'

        loop = 1
        while loop == 1
                printf(">> ")
                command = gets.chomp
                if command == "quit" then
                        loop = 0
                else
                        session.exec(command)do |ch, stream, data|
                          puts "[#{ch[:host]} : #{stream}] #{data}"
                        end
                end
        end
end

The problem I have at the moment, is when I enter a command in the interactive prompt, the "session.exec" does not return the output util I quit the program, I was wondering if anyone has come across this problem and can tell me how I can go about solving this problem?

A:

Adding session.loop after session.exec allows the program to wait for the output.
Such as: 
session.exec(command)do |ch, stream, data|
  puts "[#{ch[:host]} : #{stream}] #{data}"
end

session.loop
# Or session.wait also does the same job.

