var app = require('http').createServer(),
    io = require('socket.io').listen(app),
    exec = require('child_process').exec,
    spawn = require('child_process').spawn,
    fs = require('fs');

exec('IRBRC=\'irb.rc\' irb', function(error, stdout, stderr) {
    console.log(error);
    console.log(stdout);
    console.log(stderr);
});

io.of('/ruby').on('connection', function(socket) {

    var ruby,
        socketOn = false;

    socket.on('codeBuild', function(data) {
        console.log(data);

        var fileSaveError,
            codeLoadError;

        // Save file
        fs.writeFile('games/game.rb', data.fileContent, function(error) {
            fileSaveError = error;

            // Emit file saved
            socket.emit('buildStatus', {
                output: error || 'Code saved.'
            });

            // Check file syntax?

            // Start IRB
            ruby = spawn('irb');

            ruby.stdout.setEncoding('utf8');
            ruby.stderr.setEncoding('utf8');

            // Funnel file into IRB
            socketOn = false;
            ruby.stdin.write(data.fileContent + '\n', function(error) {
                codeLoadError = error;

                // Emit code loaded
                socket.emit('buildStatus', {
                    output: error || 'Game loaded.'
                });

                if (!fileSaveError && !codeLoadError) {
                    socket.emit('buildStatus', {
                        output: 'Build successful!'
                    });
                } else {
                    socket.emit('terminalError', {
                        output: 'Build failed!'
                    });
                }
            });

            // Bind events on IRB
            ruby.stdout.on('data', function(data) {
                console.log('stdout: ' + data);
                if (socketOn) {
                    console.log('socket on');
                    socket.emit('terminalOutput', {
                        output: data
                    });
                }
            });

            ruby.stderr.on('data', function(data) {
                console.log('stderr: ' + data);
                socket.emit('terminalError', {
                    output: data
                });
            });

            ruby.on('close', function(code) {
                console.log('Exit code: ' + code);
            });

        });
    });

    socket.on('terminalInput', function(data) {
        console.log(data);
        socketOn = true;
        if (ruby) {
            ruby.stdin.write(data.input + '\n');
        }
        else {
            socket.emit('terminalError', {
                output: 'Game not built!'
            });
        }
    });

    fs.readFile('games/game.rb', function(err, contents) {
        console.log(contents);
        socket.emit('ready', {
            output: 'Ready!',
            fileContent: contents.toString()
        });
    });
});

app.listen(process.env.PORT || 8888);

@@ -82,50 +82,8 @@
 p),%0A
-    exec = require('child_process').exec,%0A
     
@@ -151,150 +151,8 @@
 );%0A%0A
-exec('IRBRC=%5C'irb.rc%5C' irb', function(error, stdout, stderr) %7B%0A    console.log(error);%0A    console.log(stdout);%0A    console.log(stderr);%0A%7D);%0A%0A
 io.o
@@ -669,16 +669,143 @@
 yntax?%0A%0A
+            // Kill old IRB%0A            if (ruby) %7B%0A              oldRuby = ruby;%0A              oldRuby.kill();%0A            %7D%0A%0A
         

Fix potential memory leak