# REPL in a browser

**URL:** <https://www.ruby-forum.com/t/repl-in-a-browser/174578>\
**Category:** Rails\
**Created:** [August 29, 2009, 12:07am UTC](https://www.ruby-forum.com/t/repl-in-a-browser/174578 "2009-08-29T00:07:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris\_the\_frog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bbce88/32.png) [@chris\_the\_frog](https://www.ruby-forum.com/u/chris_the_frog)\
**Post date:** [August 29, 2009, 12:07am UTC](https://www.ruby-forum.com/t/repl-in-a-browser/174578/1 "2009-08-29T00:07:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chris\_the\_frog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bbce88/32.png) [@chris\_the\_frog](https://www.ruby-forum.com/u/chris_the_frog)\
**Post date:** [August 29, 2009, 2:11am UTC](https://www.ruby-forum.com/t/repl-in-a-browser/174578/2 "2009-08-29T02:11:42Z")

</div>

On Aug 28, 4:44 pm, chris\_the\_frog [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://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](http://www.sinatrarb.com)  
home page really is all you need for a working Sinatra app).

---

<div class="post-metadata">

**Author:** ![chris\_the\_frog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bbce88/32.png) [@chris\_the\_frog](https://www.ruby-forum.com/u/chris_the_frog)\
**Post date:** [August 30, 2009, 2:01am UTC](https://www.ruby-forum.com/t/repl-in-a-browser/174578/3 "2009-08-30T02:01:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chris\_the\_frog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bbce88/32.png) [@chris\_the\_frog](https://www.ruby-forum.com/u/chris_the_frog)\
**Post date:** [August 29, 2009, 6:20pm UTC](https://www.ruby-forum.com/t/repl-in-a-browser/174578/4 "2009-08-29T18:20:55Z")

</div>

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

> **[GitHub - whymirror/TryRuby: Try Ruby is \_why's interactive ruby shell that ran...](https://github.com/whymirror/TryRuby/tree/master)**
>
> master

–Matt J.
