I played around with getting Ruby to work in the browser while watching some cheesy TV this weekend.
I was able to get things slightly working, and put this work in progress up on Google Code here.
That link sends you to a page that will evaluate Ruby for you, both from a script tag, and from your own input.
If you take a look at the top of the file, you will see:
<script type="text/ruby"> #class Foo; def initialize; @foo = 'whee'; end; end; f = Foo.new; f Time.now </script>
This script is found by the Ruby in the browser system, and is run through JRuby via a little applet. The page takes the output at the end and throws it into the container HTML on the right.
You can also edit the text area at the bottom left and run your own Ruby code, and the output will be placed on the right too.
Now, this is just the first baby step to get to something feature full like:
<script type="text/ruby"> document.ready do |dom| dom["table tr"] <<"<td>test</td>" end </script>
Just being able to run some Ruby doesn’t mean much. We need to give it rich support for the browser to be able to interact with the DOM and Ajax libraries to do cool things, using the power of Ruby.
So, there are a lot of TODOs here:
- The applet is a download of JRuby complete, which is huge, and isn’t all needed. I need to work with the JRuby folk to produce a slimmed down applet version.
- Come up with a solution that allows you to puts data out (maybe put that data in a div somewhere), and more importantly, talk to the DOM directly from Ruby. Since the applet can contain Ruby files themselves, we can create a Ruby module that lets you do DOM stuff, that becomes JavaScript which will be eval’d to run in the browser. Or, we just give direct access. I am still playing with what makes sense there
- Only tested in Firefox. There are some known issues in Safari.
- I am lazily using an applet tag instead of the magic object/embed stuff that would make this work in other places
Any direction make sense to you?
April 16th, 2007 at 12:42 pm
I would say that puts should just to the same thing that System.out.println does in an applet, which is print to the java console. The better way to handle having the Ruby code update on the page is just use the innerHTML of the dom, using the “<<” operator, as you have shown in the example.
April 16th, 2007 at 4:17 pm
Wow, this is nice. I wonder if one thing that might work is some sort of port of RJS to this system.
I know absolutely nothing about Java, though, so I’m just thinking outloud.
April 16th, 2007 at 4:22 pm
Hi Dion,
this is awesome!! I’m one of the few guys believing sandboxed applets should be the way to the RIA for things you can’t do in simple DHTML (embedded database, powerful graphics with Swing or java2D…) and you don’t want to do in flash.
Ok, a good piece of advise: loading in the background is good. Loading with pack200 is even better! Do you know you can compress your jar (arround 5 times) so that it gets smaller than common javascript libraries?
Also, to increase the user experience, using javascript which is faster to load to handle the first user interactions untill the jvm starts might be a good solution.
I blogged a bit about that a few months ago:
about the pack200 trick: http://rvalyi.blogspot.com/2006/09/applet-revival-jgraphpad-ce-blackjax.html
also in defense of applets for RIA: http://rvalyi.blogspot.com/2006/08/degraded-ajax-app-pre-loaded-hidden.html
finally I tried to coin: BLACKJAX” for “Background Liveconnect Applet Code Kicking Javascript and XML.”: http://rvalyi.blogspot.com/2006/09/ajax-applet-blackjax.html
Let’s spread the buzz!
The diagram editor I’m talking about (JGraphpad Community Edition) isn’t anymore on my demo site because my domain expired and I have been too much busy recently. Still, you can download the app with such an applet demo here: http://www.sourceforge.net/projects/jgraph/
This is working a bit like your applet: javascript helper to build the applet tag and javascript call backs, background download and even jvm loading…
And there is the pack200 trick, may be some ideas to catch (even if I should admit I was beginning with javascript last time I made the js helper).
Now, I’m very impatient about JRuby in the browser. As soon as I get time again, I’ll build a generic diagram driven simulator (for every sort of languages) based on JGraphadCE. I already made it partially using pure Java and the Fractal Aid math (http://www.fraid.org) language for “emergy simulator” (again sorry for the site trouble). But I couldn’t do much, Ruby is actually much better to build DSL.
So I’m waiting for JRuby in the browser to put it as an additional layer on the top of JGraphadCE.
Also, there are things where some JRuby/Prototype communication could be a big win: overcome applets securties restrictions to save on the disk (by poping a file to download with the browser), consume cross domain json with padding web services, use the browser threading to do the AJAX (seems to freeze applets in some way)… On the contrary, the browser will get sockets, Java2D, ruby language…
By by the Microsoft, distributing crappy jvms and blocking SVG won’t be enough…
April 16th, 2007 at 5:35 pm
From my previous searches I thought “text/x-ruby” was the best mimetype to use for ruby, not text/ruby. Did you just pick text/ruby as a reasonable guess or do you know what the authority is on this?
April 16th, 2007 at 5:43 pm
Hi Tor,
text/x-ruby probably is better, or application/x-ruby? or ….
I am more than willing to choose something else that makes sense. I just ran with text/ruby to be simple.
Cheers,
Dion
April 16th, 2007 at 5:45 pm
Rapha
April 16th, 2007 at 6:02 pm
I think text/x-ruby is nicely symmetric with for exampe text/x-java, and it expresses that Ruby source is text.
April 16th, 2007 at 6:07 pm
Tor,
I just added support for text/x-ruby and changed the page to use it.
Thanks for contacting me.
Cheers,
Dion
April 16th, 2007 at 7:46 pm
Congrats. I took a look at doing this in Februrary and had some applet challenges with the JRuby code. There is a documented way for Java to communicate with the JavaScript engine. It’s even referred to in the JRuby example applet code, I think.
April 16th, 2007 at 8:49 pm
Really nice Dion! I did something similar with Groovy, but I’m a step behind as my version does not uses a script tag, but the direct applet declaration. Ruby/Groovy whichever the case, I think having other choices besides JavaScript on the browser is a Good Thing, at least on well controlled environments as ‘intranet’ enterprise applications.
April 17th, 2007 at 2:26 am
The problem with ‘text/*’ mime types is that they default to US-ASCII encoding if the ‘charset’ parameter is missing (and it usually is). Thus, the ‘application/*’ sub type is probably more suitable if you want your Ruby code to contain non-ASCII characters. To use ‘application/ruby’, though, you have to register the type with IANA through an RFC. It would do Ruby well to have a MIME type in any case, so pushing the Ruby community to write the RFC would be a good thing.
April 17th, 2007 at 4:39 am
Interesting stuff! :)
I guess you could plug into the applet jar-caching stuff; so the jars for things like jruby are only downloaded once on startup right?
I wonder if all the applet/object/embed muck could be hidden with JavaScript/CSS to keep the HTML nice and simple, like the way folks tend to bind JavaScript to HTML markup using CSS styles?
Incidentally, this clearly isn’t a ruby specific solution; folks could use ruby, groovy, beanshell, scala, python or whatever else floats their boat.
Also folks could actually rely on things like E4X
http://www.ecma-international.org/publications/standards/Ecma-357.htm
April 17th, 2007 at 5:59 am
Very nice. Will this work with IE?
Perhaps you could extract classes out of the jars that aren’t *always* needed. That way, they would be downloaded as needed.
A completely different solution is to translate Ruby to JavaScript similar to how GWT converts Java to JavaScript. Work has been done on this:
http://po-ru.com/diary/convert-ruby-to-javascript
April 18th, 2007 at 9:06 am
This is great. I saw that it didn’t work with Safari, but the fix was easy. Seems to work if you construct the applet tag as a string instead of using setAttribute(), like:
applet: function() {
var t = ‘<applet name=”RubyApplet” archive=”jruby.jar,asm.jar,backport-util-concurrent.jar” codebase=”classes” code=”RubyApplet”></applet>’;
var applet = document.createElement(’div’);
applet.setAttribute(’id’, ‘applet-wrapper’);
applet.innerHTML = t;
document.body.appendChild(applet);
}
April 22nd, 2007 at 5:18 pm
Dion, I’ve updated my Groovy version “borrowing” your trick for tag handling. You’ll find more info at http://jroller.com/page/aalmiray/?entry=more_on_groovy_applets
Cheers!
May 14th, 2007 at 3:23 pm
I wish there were a Ruby/Tk for the browser — like…run a Tk window there (or rather some type of GUI) so you could run a Rubyplet :) That would be sweet.
September 9th, 2007 at 6:18 am
download doom2 [url=http://freesexvidslists2.za.pl]download doom2[/url]
dvdxcopy [url=http://freesexvidslists2.za.pl]dvdxcopy[/url]
hotmovies.com card [url=http://freesexvidslists1.za.pl]hotmovies.com card[/url]
free advertizing sites [url=http://freesexvidslists2.za.pl]free advertizing sites[/url]
free pinkworld porn [url=http://freesexvidslists1.za.pl]free pinkworld porn[/url]
5.9 aim download [url=http://freesexvidslists1.za.pl]5.9 aim download[/url]
movietickets.com [url=http://freesexvidslists1.za.pl]movietickets.com[/url]
free backround pictures [url=http://freesexvidslists1.za.pl]free backround pictures[/url]
genital massages [url=http://freesexvidslists1.za.pl]genital massages[/url]
burner cd download rar win [url=http://freesexvidslists2.za.pl]burner cd download rar win[/url]
nikki sixx site myspace.com [url=http://freesexvidslists2.za.pl]nikki sixx site myspace.com[/url]
leprecon movies [url=http://freesexvidslists2.za.pl]leprecon movies[/url]
the free project vouyer [url=http://freesexvidslists2.za.pl]the free project vouyer[/url]
yahoo booter download [url=http://freesexvidslists1.za.pl]yahoo booter download[/url]
alzheimers association eldercarehelpresources.com [url=http://freesexvidslists1.za.pl]alzheimers association eldercarehelpresources.com[/url]
zango free games [url=http://freesexvidslists2.za.pl]zango free games[/url]
downloadspeed [url=http://freesexvidslists2.za.pl]downloadspeed[/url]
crochet slipper pattern free [url=http://freesexvidslists1.za.pl]crochet slipper pattern free[/url]
download phim nguoi lon mien phi [url=http://freesexvidslists2.za.pl]download phim nguoi lon mien phi[/url]
downloadgames [url=http://freesexvidslists2.za.pl]downloadgames[/url]
free desktop dancers [url=http://freesexvidslists2.za.pl]free desktop dancers[/url]
winks for msn download [url=http://freesexvidslists1.za.pl]winks for msn download[/url]
download music pual sean [url=http://freesexvidslists1.za.pl]download music pual sean[/url]
dope wars download [url=http://freesexvidslists1.za.pl]dope wars download[/url]
download free nero startsmart [url=http://freesexvidslists1.za.pl]download free nero startsmart[/url]
brooke hogan download [url=http://freesexvidslists1.za.pl]brooke hogan download[/url]
moviepass.com [url=http://freesexvidslists2.za.pl]moviepass.com[/url]
free ecards funny animated [url=http://freesexvidslists2.za.pl]free ecards funny animated[/url]
masturebation movies [url=http://freesexvidslists2.za.pl]masturebation movies[/url]
glassjaw lyric [url=http://freesexvidslists1.za.pl]glassjaw lyric[/url]
September 28th, 2007 at 11:46 pm
zdPwUm qpfmfcfvgmfl, [url=http://skvfbexjejxm.com/]skvfbexjejxm[/url], [link=http://nscosvafmakh.com/]nscosvafmakh[/link], http://ktxqqurlpnse.com/
October 3rd, 2007 at 5:33 am
He learned about justice or rather injustice early in his life. Cesar grew up in Arizona; the small adobe home, where Cesar was born was swindled from them by dishonest Anglos. Cesar’s father agreed to clear eighty acres of land and in exchange he would receive the deed to forty acres of land that adjoined the home. The agreement was broken and the land sold to a man named Justus Jackson. Cesar’s dad went to a lawyer who advised him to borrow money and buy the land. Later when Cesar’s father could not pay the interest on the loan the lawyer bought back the land and sold it to the original owner. Cesar learned a lesson about injustice that he would never forget. Later, he would say, The love for justice that is in us is not only the best part of our being but it is also the most true to our nature.
October 9th, 2007 at 7:27 am
Need to sell used car?
Need to sell car?
many deals on http://sell-used-car.info/sell+my+car7skey/
May 18th, 2008 at 11:57 pm
thanks for sharing!
June 2nd, 2008 at 10:20 am
very good
buy xanax
June 23rd, 2008 at 2:28 am
good.Thank you!
July 9th, 2008 at 1:26 am
Find really cheap airline tickets, Dirt cheap airline tickets at tickets for tomorrow. We have a list of really cheap international airline tickets, and dirt cheap airline tickets, cheap airline tickets. Book your very cheap airline tickets at http://www.ticketsfortomorrow.com
November 8th, 2008 at 8:29 pm
That is awsome dude! Nice work, and thanks for sharing this experiment!
November 10th, 2008 at 3:51 pm
What version of Java is required to run this applet?
There was a bit of press lately about the move away from client-side Java by the folks from sharendipity, because it was apparently difficult to get Java working correctly for some users.
Ignoring the Java installation hurdles, this is actually a pretty neat experiment. Now I wonder if anyone will do a Rhino version, just for the kicks – or perhaps for js 1.9 support in IE :)
February 10th, 2009 at 3:30 am
Hi,
Yes you really have a good content.
April 24th, 2009 at 6:38 am
This is awesome! I want to run Ruby in the browser so i will look at your code as soon as i get back home. I was wondering how to get something ruby-ish working in browser and it took me 5 days to realize there is JRuby!
Thank man!
May 14th, 2009 at 12:09 pm
What would it take to get this talking to the DOM and able to run browser-side Javascript? I would be very interested in getting something like this working as soon as possible for a project of mine.
May 27th, 2009 at 10:02 am
Cesar learned a lesson about injustice that he would never forget. Later, he would say, The love for justice that is in us is not only the best part of our being but it is also the most true to our nature
May 29th, 2009 at 2:04 pm
Hi there,
I actually started work on something similar – I ran into a little problem, and I’m thinking you may as well: I’m guess that the Applet you have loads the ruby scripts after it (the applet) has loaded, perhaps using the DOM or JSObject to locate the tags. This works fine under FireFox, but I found under IE the entire contents of tag are completed dropped – nothing is returned.
May 29th, 2009 at 2:27 pm
Sorry, forget my last post :P I got it working – just needed to re-implement the DOM access (which also now gives full DOM access).
July 27th, 2009 at 9:07 am
There was a bit of press lately about the move away from client-side Java by the folks from sharendipity, because it was apparently difficult to get Java working correctly for some users.
October 7th, 2010 at 2:49 pm
Now my question is…is there a way to manipulate what the browser displays (like emit javascript) using this…kind of like what gestalt does (can manipulate the dom)…
October 7th, 2010 at 2:52 pm
maybe use rails’ RJS stuff to be able to manipulate the DOM easily
http://wiki.rubyonrails.org/howtos/rjs-templates
October 25th, 2010 at 11:04 pm
nice work
dose anyone do this thing in python?
and
dose this thing work in HTA?