Wednesday, November 15, 2006

Ruby for the Web? Check!

Well friends, it's time for another episode of "Impossible Or Not!" Today's contender is Ruby in the browser, long desired but never achieved. There are front-ends to Ruby services, delicious Ruby-JavaScript libraries, and of course the ever-popular Ruby on Rails web framework. However, developers have been clamoring for something more.

IRB in an Applet

A long, long time ago in a web far away, there was born a bright-eyed new child named Java. Java found its first public uses in flashing, twirling, annoying buttons called Applets. It was also a bit slow, having just been released into the world. So the big bad public said "Java is slow and only useful for annoying buttons!" And so Java was branded the "slow annoying button" language.

But Java has grown up. It hid away from the public eye, dwelling in dark, dank servers and enterprises. It learned the value of five nines uptime and horizontal scalability. All the while, it improved its public face, preparing for a return home to its birthplace on the web.

Now Java has grown up. It has learned how to appease the enterprise gods while presenting itself to the web in beautiful, performant glory. And it has a new friend: Ruby.

Yes, JRuby can run in an applet. No, it's not that hard. No, the archive doesn't have to be this big (the applet above is about a 1.6MB JAR file, but it includes stuff it doesn't need). Yes, this means you could start writing stuff for web pages in Ruby. No, I'm not kidding.

Yes, Virginia, there is a Santa Claus.

Updated: I fixed the issues under Windows, so it should work for those of you that reported errors. Thanks for the heads up!

14 comments:

  1. Love your work, but...
    Couldn't run applet:

    Java Plug-in 1.5.0_06
    Använder JRE-version 1.5.0_06 Java HotSpot(TM) Client VM
    Användarens hemkatalog = C:\Documents and Settings\mikael.pahmp



    en_US: /res/dldir_en.res
    java.lang.IllegalArgumentException: Neither current working directory (/) nor pathname (builtin/etc.rb.rb) led to an absolute path
    at org.jruby.util.JRubyFile.create(JRubyFile.java:51)
    at org.jruby.runtime.load.LoadService.findFile(LoadService.java:349)
    at org.jruby.runtime.load.LoadService.findLibrary(LoadService.java:278)
    at org.jruby.runtime.load.LoadService.smartLoad(LoadService.java:205)
    at org.jruby.Ruby.init(Ruby.java:506)
    at org.jruby.Ruby.newInstance(Ruby.java:225)
    at org.jruby.JRubyApplet.start(JRubyApplet.java:32)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    java.lang.IllegalArgumentException: Neither current working directory (/) nor pathname (builtin/etc.rb.rb) led to an absolute path
    at org.jruby.util.JRubyFile.create(JRubyFile.java:51)
    at org.jruby.runtime.load.LoadService.findFile(LoadService.java:349)
    at org.jruby.runtime.load.LoadService.findLibrary(LoadService.java:278)
    at org.jruby.runtime.load.LoadService.smartLoad(LoadService.java:205)
    at org.jruby.Ruby.init(Ruby.java:506)
    at org.jruby.Ruby.newInstance(Ruby.java:225)
    at org.jruby.JRubyApplet.start(JRubyApplet.java:32)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception in thread "thread applet-org.jruby.JRubyApplet.class" java.lang.NullPointerException
    at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
    at sun.plugin.AppletViewer.showAppletException(Unknown Source)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

    ReplyDelete
  2. +1 previous comment. Looks like the same exception I'm getting.

    ReplyDelete
  3. Well, you can't win 'em all! I have a theory that it may be a localisation problem for you three that have reported an error. Can you contact me offline at charles DOT nutter AT NOSPAM sun DOT com and help me figure out why it doesn't work for you?

    ReplyDelete
  4. It works! :) But the bottom of the applet is kind of truncated, don't know if it's a Konqueror problem. I made a screenshot: http://misto.ch/images/jruby_applet.png

    ReplyDelete
  5. paul: If you can from an applet, then you should be able to from JRuby. However I don't know how to do such things, so there's some research needed there. It would be pretty fun to use JRuby as a client-side scripting language in place of JavaScript.

    ReplyDelete
  6. It's interesting to note that it would be (comparitively) trivial to bulid a JavaScript library which adds the applet tag and looks through the DOM tree for any <script language="ruby">...</script>, passing the contents to the applet. The applet could then run the contents (in our ever favorite JRuby). Special stuff like access to the DOM tree or confirm() could be done in a new module, Browser which could be automatically included with all Ruby script. The only downside is that you would be forced to lug around a fairly sizable JAR file. (when I say fairly, I mean fairly, not overly bloated) :-) The extra size of the JAR probably isn't worth the syntactical beauty that you'd get from being able to do client-side AJAX in Ruby.

    ReplyDelete
  7. You tell JRuby is 1.6 M to embed in an applet... But do you know about Pack200? Hey, with Pack200, I'm pretty sure you could divide by 4 that download while serving it transparently to java 1.4 clients (no pack200 client) or to java 1.5+ clients...

    with Pack200, java applets or webstart apps are a way faster to download than javascript equivalent ones... Only the cold start JVM slow startup keeps sucking, but it's getting less and less noticeable as powerful CPU come on the desktop...

    ReplyDelete
  8. stug: The issue should be fixed; for another post I uploaded a different jruby-complete.jar that interfered with the applet. I've replaced it now.

    ReplyDelete
  9. Thanks!!! this should be handy for.. lets say.. ummm.. uhhh... ummmm.... uhhhmmmmmm uhmmmmmm auhhhhhh.... hurrummph...

    oh well, time well spent. onto the next ruby trick! how about running ruby running on molasses.. yes and how about a Ruby Interpreter for MUD!

    yay! now we can run ruby on MUD! not to mention concret, cow-dung and garden variety compost!

    Very Very handy! I love this stuff.. keep cranking this stuff out guys! you're onto something!

    something completely useless!

    ReplyDelete
  10. Thanks, this looks like an awesome little applet to have some of my colleagues try ruby without them having to install anything!

    ReplyDelete
  11. I'd actually being interested in using this technology, if it would let me make requests to other domains from within the applet. perhaps that is a sercurity exemption the applet could request from the user... I know little about Java, but the idea of putting little brains out there on the client side seems wonderful.

    ReplyDelete