Rainy days and Java always get me down


I've Moved My Blog

It's currently located at http://www.urlinone.com/blog

I should say "I'm moving my blog." It's a pretty painful process.

Pebble has blown up on me, and it's been many months since I've been able to blog reliably. I've lost posts. And now I've got to figure out how to migrate my past blog posts from Pebble to my new destination without all the URLs changing, lest external links become 404 Not Founds.

Why does everything in the 21st century have to be a three-day project???

AuctionRelay.com has been running in Tomcat for a few years. A simple "include" script tag is all it takes to make AuctionRelay add HTML to the middle of an existing web page. The trick is in using the document.write command to make executable JavaScript write visible HTML at the location in the document where it executes.

I'm now in the process of rewriting AuctionRelay to run on Ruby on Rails (shouldn't that really be Rails on Ruby?). This morning, it was time to tackle the issue of setting the HTTP Content-Type header in the response, so that the browser would recognize that the response was executable JavaScript, and not displayable HTML. (Of course, the executable JavaScript is responsible for squirting in the displayable HTML, but that's the kind of convoluted world we've created for ourselves.)

I can browse to http://localhost:3000/js/test, which includes the line

<script src="http://localhost:3000/js/free?id=option_insight"/>
and see something like this:
in the midst of the original HTML page. (What you see here is still the output of the Java version.)

js_controller.rb:

class JsController < ApplicationController

  def out
    @sellerId = @request.params['id'].to_s
    $mylog.debug("free request for " + @sellerId)
    
    if $freeSellers[@sellerId].nil?
      $mylog.debug("Not found - instantiating...")
      $freeSellers[@sellerId] = Ebay.new
      $freeSellers[@sellerId].retrieve(@sellerId) 
    else
      $mylog.debug("Already exists.")
    end
    
    @headers["Content-Type"] = "text/javascript"
  end

  def test
  end
end

test.rhtml:

Before
<script src="http://localhost:3000/js/out?id=option_insight"/>
After

out.rhtml:

document.write('<table border=1 align=center><caption><br/>My eBay feedback rating is\
<a href="http://feedback.ebay.com/ws/eBayISAPI.dll?ViewFeedback&userid=<%= @sellerId %>" target="_blank">\
<%= $freeSellers[@sellerId].feedback %>\
</a><br/></caption>\
<tr><th>Item<th>Price<th>Bids<th>Ends<tr>\
<td colspan=4 align=center>Display your live auctions on any web site for free at\
<a href="http://www.auctionrelay.com" target="_blank">AuctionRelay.com</a></table>');

One of the things I learned the hard way is that the string you pass to document.write must either be on a single line or use backslashes for continuations. (I was trying to make out.rhtml readable. Duh. Hindsight is 20/15 here.) Fortunately, the Firefox JavaScript Console gave me an error message that made me realize what was going on. Unfortunately, it took me 15 minutes of head-scratching to even think to look at it and recognize that the problem was with the actual JavaScript syntax and not something server-side.

Microsoft Office Outlook 2003. A product so advanced that it understands time coordinates from other galaxii.

When this ingenious product pops up a reminder for you, did you know that you can snooze it, not only with the drop-down list of options from minutes to weeks, but that you can also type in an arbitrary floating point amount of time to snooze? You can type in 1m, 5.3 d, 2w, etc. (One problem is that I'm not sure if 1m means I'll be reminded again in a minute or a month. But that's my fault for not RTFM.) But I discovered today that you can also type in 1frdtydangy.

Interestingly, it gave me an error when I tried frdtydangy. Naturally. How can you expect it to delay based on a lone unit of measure, if you don't tell it how many of that unit of measure you mean? I mean, it's not like Outlook 2003 can read your mind or something, right? No, it wasn't until I added a leading 1 that Outlook 2003 understood that 1frdtydangy meant I wanted this particular reminder to snooze for the amount of time it takes the moon of wklcphhhftzsh-po to complete one full revolution around it's neutron star.

I hope it's less than a frdtydangy before I can install OS X on my laptop.

Let's say you define a simple Hash.

irb(main):001:0> h1 = {"a"=>1,"b"=>2,"c"=>3}
=> {"a"=>1, "b"=>2, "c"=>3}

What do you think you'll get when you send an each message to it? I figured either a, b, c or 1, 2, 3. Surprise:

irb(main):003:0> h1.each{|a| puts a}
a
1
b
2
c
3
=> {"a"=>1, "b"=>2, "c"=>3}

It all comes out, keys and values interleaved.

Now, the trick becomes figuring out what to Google for to learn the syntax that gets you just the part of the hash you're after. Let me save you a little time. What you're after is each_value.

irb(main):008:0> h1.each_value {|a| puts a}
1
2
3
=> {"a"=>1, "b"=>2, "c"=>3}

You can also use each_key. Fairly intuitive, once you know it. Ah, there's the rub. :-)

irb(main):009:0> h1.each_key {|a| puts a}
a
b
c
=> {"a"=>1, "b"=>2, "c"=>3}

Working for an integration company has changed my perspective on things. I used to be totally web-centric, focused on AppFuse, Tapestry, Spring, Hibernate, etc. While I still have interest in those things, my horizon has expanded beyond that single application silo.

Ruby on Rails interests me a lot, but I keep having fear, uncertainty, and doubt about the platform and supporting infrastructure. I know that a lot of bile has been spilled over Rails versus various J2EE frameworks, but I believe that a lot of that is fueled by fear. People who've invested the last several years honing a skill don't like to hear that it's being obsoleted by the next new thing. Ruby and Rails look like a threat, so some people want to discredit it. (If you object to that point, you're probably one of them. ;)

But my fear is not based on that; I've never been the type. I tend to throw myself into the unknown with great abandon. I left behind a guru-like knowledge of embedded, real-time C++ (I could tell you what instructions the compiler would generate from a given section of code; I could convert IEEE floating point format in my head; it's quite sick, really) in the world of avionics for the great unknown of J2EE. I didn't even know what an application server was at the time I started at ATG. Thank you, crazy tech bubble, for making my career change possible. And now I find myself in the land of SOA and ESB. No one can accuse me of being complacent.

My fear of Ruby is based on investing time and energy in a technology that never pans out and leaves me hanging, half-way through. Being a sucker for bright, shiny things, I worry that I can't trust myself to assess the real value of this new bauble.

So, today (really for the past couple of months), I'm weighing Ruby and Rails. I've never really liked Java syntax, and the efficiency of Ruby feels like a return to my lean and mean C/C++ roots, where men are men and, you can assign a value to a variable at the same time that you test it in an if-statement. (I like a lot of rope.) I believe in code generation, and Rails does a nice job of providing a quick way through the tedium. But the thing that keeps me up at night is the platform.

Is the Ruby runtime anywhere near the quality of fourth and fifth generation JVMs? How's the threading model? Garbage collection? JIT compiling?

And even if you don't want to be a totally anal-retentive nerd, dinging the VM for academic shortcomings, what about interoperability? I would imagine that you could fairly easily implement things like JMS and JMX in Ruby, but they don't exist today. I can think of so many things I could do with Ruby if it offered such technologies. Without them, I feel hesitant to commit, as much as I am ready to fall in love. Do I want to limit myself to a platform that can only serve basic database-backed web apps (no matter how quickly or nicely)?

The question remains whether Ruby will grow up to be a platform that can displace Java. Java has accomplished some remarkable things. It wasn't the first bytecode interpreted language, but it was the one that stuck. It wasn't the first environment to run on multiple platforms, but it is the one that was ported to mainfames and smartcards. It wasn't the first language to promote code reuse, but I've never seen anything like its vast library of reusable code in any other language (which is also a source of great pain, but that's a topic for another JCranky post). You can run JRuby in a JVM, but you can't run Java in Ruby. The JVM is a formidable incumbent.

Java is not just a language, it's an environment. And so must Ruby be. The question is whether Ruby's environment can grow up to be as robust, rich, and performant as the JVM. I really hope so. I could say goodbye to Java the language with no regrets. I can't say the same thing about Java the platform.

C'mon, Ruby, I'm pullin' fer ya! Don't let me down.

I'm not sure why every language feels the need to use its own keywords. What would be the crime if all languages used throw, raise, and catch?

Anyway, if you want to examine the Exception object in Ruby, in the rescue line, just add => e as in

    rescue => e

Now, the local variable, e, contains the Exception object, which you can log:

    $mylog.debug("In Rescue block: " + e)