REPL in a browser

Hi! I am a computer science student and I have a problem I just can’t
seem to find an answer to. I would like to embed a read-eval-print-
loop in a website. Essentially, I’d like a window that mimics a
console: the user types in some random text, it is processed, a value
is returned and displayed, and then it waits for the user’s input
again*. Exactly like a console.

I am new to Ruby and I have only just begun looking at Rails. In fact,
I have absolutely no experience in web programming whatever. So even
if someone could just point me in the right direction, I’d appreciate
it.

  • FYI, I am trying to create an online version of my research project:
    an interpreter and interactive environment for a small functional
    language.

On Aug 28, 4:44 pm, chris_the_frog [email protected] wrote:

it.

  • FYI, I am trying to create an online version of my research project:
    an interpreter and interactive environment for a small functional
    language.

I can’t imagine Rails offering any aid in this endeavour; if you’re
just taking user input to pass to a custom language interpreter, a
basic CGI script will do quite nicely (or maybe if you absolutely want
to get the lightweight stuff out of the way a much smaller framework
like Camping or Sinatra). Let me ask, do you already have your
language written? If so, I presume you used something other than Ruby,
so I’d probably be easiest to just write the web REPL in the used
language and pass the user’s input as a string to to your interpreters
routines. Otherwise, using Ruby to call your language’s interpreter
executable safely may prove difficult, as the easiest way to capture
output of an external command (backticks ` or %x{}) could make it
extremely difficult to sanitize input, and I know of no way to
redirect the output of the multi-argument version of Kernel#system
(which bypasses shell escaping). Eitherway, if anything malicious is
possible with your language, you’ll first need to create a sandboxed
version of your interpreter before even considering building a web
frontend.

For just learning how to write a CGI script with Ruby though, the
standard documentation itself is quite explanatory (http://www.ruby-
doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html), and most frameworks
come with fantatsic documentation as well (hell, the text on
www.sinatrarb.com
home page really is all you need for a working Sinatra app).

If just want to process some basic commands in a web environment you
could create a form which simply re-displays itself.

Setup a class variable (or simply use the flash notification) to hold
the result of whatever you want to do and instead of redirecting to
show or index, redirect to new. Then display it in your view. You can
assign a blank value for the first run. A simple model could hold the
previous entries and you could iterate over this in the view if you
wanted to show history.

One word of caution though, make sure you have a good grasp on the
security implications before you expose your computer in this fashion.
One bad file i/o call can ruin what might have been a good day.

You may want to take a look at the resurrected TryRuby repo - that
tool was essentially exactly what you’ve described.

–Matt J.