#! /usr/bin/ruby

# mini record-n-playback for ruby source code
# (to enter ruby code and run what you've entered so far)

# local vars
terminator = "Q"         # special line of input used to end this script
playback = ""            # special line to run what has been entered so far
sourcecode = String.new  # the program so far
userinput = String.new   # the most recent line of input
nextline = String.new    # a stripped version of the userinput line

# give the user instructions
puts "This program allows you to enter lines of source code,"
puts "   storing all your code in a single program,"
puts "and running the program (so far) each time you enter a blank line"
puts "\nEnter #{terminator} to quit"

# keep going until they choose to quit
until nextline == terminator

   # get their next line of input
   userinput = gets

   # if they quit then show them their final code,
   # or if it's a blank line then run their code so far,
   # otherwise add their new code to the program so far
   nextline = userinput.strip
   if nextline == terminator then
      puts "Your final program source is\n#{sourcecode}"
   elsif nextline == playback then
      puts "---Running code---"
      eval sourcecode
      puts "----End of run----"
   else
      sourcecode += userinput
   end
end

