title: How does using inifinite loops help this Ruby psuedoterminal capture input and output?
body: <p>Goal: I am trying to turn text-messaging on my iphone into a ruby repl, and I hit upon psudoterminals as a way to communicate between my sms-recieving web app I built and irb on the command line.</p>
  <p>I found a ruby wrapper that runs an interactive command line program using psuedoterminals, and I'm very puzzled as to how it actually works. From the user's perspecitive, it acts exactly like irb, except I launch it by calling</p>
  <pre><code>ruby interactive.rb
  </code></pre>
  <p>Instead of</p>
  <pre><code>irb
  </code></pre>
  <p>I still have to type input with my hands and get output with my eyes, but this ruby wrapper is a first step towards doing what i want to do, which is hooking the inputs and outputs up to my text messaging server.</p>
  <p>The trouble is that I do not understand what is going on at all in my 'run this interactive command line program' ruby wrapper. I found it on stack overflow and have been poking at it all day, and I still don't understand the concepts behind what it is doing. Because I don't understand it, I'm having a devil of a using it to programmatically feed inputs and output to irb.</p>
  <p>Here is the code.</p>
  <pre><code>require &quot;pty&quot;
  
  PTY.spawn(&quot;irb&quot;) do |read, write|
      Thread.new do
          loop do
              write.print STDIN.getc.chr
          end
      end
  
      loop do
          print read.sysread(512)
          STDOUT.flush
      end
  end
  </code></pre>
  <p>I adapted it from this <a href="https://stackoverflow.com/questions/2730642/wrapper-around-bash-control-stdin-and-stdout/2736354#2736354">lovely stack overflow snippet</a></p>
  <h2>What I think is going on</h2>
  <ul>
  <li>PTY.spawn(&quot;IRB&quot;) makes a psuedoterminal, which for my purposes is exactly like a command line program except I can use my script to stuff things into it and get things out of it.</li>
  <li>The two block variables, |read, write| are places where I can stick words in and get words out. Specifically, I write a command into the block variable named 'write.'  That is equvalent to me typing something and pressing enter when I am in my interactive command line program.  Then I get some stuff back.  I get it back by looking at the text in the read variable.  To clarify, I'm going to restate this in technical terms I just learned today: read and write are pipes. Read is the stdin for the psuedoterminal,  Write is the stdout and stderr for the psuedoterminal.</li>
  <li>Now things get really confusing. This program makes a 'Thread' (Whatever that is.)  The bit of code inside it is somehow responsible for capturing what I type. The loop looks a little bit like an infinite loop, and what i think is going on is that everything inside this thread is constantly checking if a new character has been typed and is therefore in the write buffer.  According ot my research, this 'write buffer' is also known as the psuedoterminal's stdin buffer AKA input pipe AKA 'thingy that takes input in'.</li>
  <li>Then there's the second infinite loop, which is constantly checking to see if anything new has been put in the read buffer and therefore needs to be shown to me, the user.  (According to my research, this 'read buffer' is also known as stdout aka stderr buffer aka pipe aka output aka file)</li>
  </ul>
  <p>So what I think I have is an infinite loop that constantly checks to see if anything new has been typed in.  And I have another infinite loop that constantly checks if anything has been printed out.</p>
  <p>Is that actually what is going on? (Is this like a poor man's node.js event loop?)  Second, these loops seem to just sort of sit there and do nothing until I type something.  Are they constantly running and checking, and merely doing nothing because there is nothing to read or write, or is there some sort of trigger somehow built into pty that is triggering these loops to go check their respective pipes for stuff?</p>
  <h2>My Research</h2>
  <p>After researching all day, here is the best mental model I could come up with:</p>
  <p>pty is something called a Psuedoterminal.  According to <a href="https://devver.wordpress.com/2009/10/12/ruby-subprocesses-part_3/" rel="nofollow noreferrer">A dozen ways to start subprocesses in ruby</a></p>
  <blockquote>
  <p>It means if we’re running Ruby on UNIX, we have the ability to start our
  subprocesses inside a virtual terminal. We can then read
  from and write to that terminal as if our program were a user sitting
  in front of a terminal, typing in commands and reading responses.</p>
  </blockquote>
  <p>That's pretty much exactly what I want.  Psuedoterminals are and old concept, and they've been used for precisely this application:  a remote client (my iphone) talking to a server side process (my irb).  I even found this nifty diagram that shows pretty much exactly the program flow I'm looking for:</p>
  <p><a href="https://i.stack.imgur.com/mwRuR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mwRuR.jpg" alt="" /></a><br />
  <sub>(source: <a href="http://rachid.koucha.free.fr/tech_corner/pty_pdip_figure_4.jpg" rel="nofollow noreferrer">free.fr</a>)</sub></p>
  <p>With my mobile phone being the client, my twilio-based sms-receiving sinatra server as the telnetd, and my irb session being run through the psuedoterminal.  The image comes from an article describing exactly what I want to do...<a href="http://rachid.koucha.free.fr/tech_corner/pty_pdip.html" rel="nofollow noreferrer">in C. ( I can't read C).</a></p>
  <p>While this research helped me wrap my head around the idea that a psuedoterminal is exactly like a terminal, except I can get a ruby script to pretend to be a person typing in on it. It does not explain how the weird loops in the code work.</p>