The FUD surrounding ECMA 4 1

Posted by Jason Yates
on Saturday, November 10

UPDATE: Dave Thomas, not the Ruby one, has some good criticisms on ECMA 4 in a blog post. Brendan Eich also recently posted some news and thoughts on ECMA 4. The ECMA TG1 group has also put out a reference implementation of ECMA 4.

UPDATE2: On another note, re-reading this article my tone and tenor towards Microsoft was a little harsh, more rant than anything. I tend to have a large latent frustration with Microsoft, especially with regards to IE. I guess don’t blog while pissed off, is probably a good rule to follow.

Surprise, Surprise, Microsoft is spreading FUD about the proposed next version of Javascript, ECMA 4. The Microsofties have been purposely vague about there criticisms on ECMA 4. As this is a requirement for FUD, quoting from Wikipedia: “FUD is generally a strategic attempt to influence public perception by disseminating negative (and vague) information.”

Brendan Eich, the CTO of Mozilla, has done a good job of rebuffing the falsehoods, as he calls it. Brendan specifically targets Chris Wilson a member of the Internet Explorer dev team. Chris Wilson explained his reluctance on ECMA 4(ES4) in a blog post: “as the mashup development pattern would require interoperation of current ES3 scripts as well as supposed new ES4 scripts. With the ScreamingMonkey plan, this clearly won’t work well – as they’ll be run in different runtimes”. This is a mischaracterization of ScreamingMonkey, developed by the open source community and Mozilla, it is meant as a backup plan only to bring ES4 to IE. In the potential case Microsoft decides not to support ES4 at all. ScreamingMonkey is a solution not the problem, and it is certainly not an excuse for Microsoft to not implement ES4 in the first place. ScreamingMonkey will only be implemented if the IE team fails to adopt the ECMA 4 standard. As Brendan Eich has explained: “I’ll make a promise: if Microsoft truly embraces ES4 and ships it in an IE beta, I’ll put ScreamingMonkey on hiatus”.

Chris Wilson goes on to say, “but even without it(ScreamingMonkey) I expressed my skepticism that ES3 scripts won’t suffer if run through an ES4 engine.” It seems like Microsoft and the IE team has already made up their mind. Now ES4 is supposed to be reverse compatible with ES3. Maybe Chris Wilson is correct and it will “suffer” as he says. However, he doesn’t give any details as to why nor has anyone from Microsoft really explained there position in detail. As the saying goes, it is time to put up or shut up.

However, I think this debate is just hiding the real underlying issue. Microsoft doesn’t care about the open web. They know they missed their opportunity with Web 1.0, and that has brought them Google and company. I don’t think they want to make this same mistake again. There is an advantage here for Microsoft. Holding back ECMA 4 adoption, spreading FUD, acting like changes will break the web, and in the end making really no changes or advancements to Javascript. As big changes and advancements would cut into Microsoft’s plans with Silverlight.

And look what the last “advancement” Microsoft brought to the open web and what it did for them. XMLHttpRequest or Ajax, originally developed for IE 5 to enhance Outlook Web Access, was quickly incorporated into Mozilla 1.0 and other competing browsers. Ajax quickly exploded and has brought Microsoft nothing but grief in a powerful and growing Google platform: Google Maps, GMail, Google Docs; a slew of new competition named Web 2.0; and bunch of other web-based technologies to numerous to name, all which REQUIRE Ajax.

Ajax has seemed to help everyone, but Microsoft and it is easy to see why. With Ajax, applications which were only viable on the desktop in the past, now become possible on the web. Ajax has begun to make the web the platform, not the operating system to potential detriment of Microsoft share.

Of course, web-based technologies can’t completely compete yet. Which goes back to my original point, and explains why Microsoft would want to stifle innovation here. If the movement is towards better, faster web based applications. Microsoft wants no part, unless your using Silverlight.

Netbeans on Rails 0

Posted by Jason Yates
on Monday, October 22

I’ve never given Netbeans a look before, as I’m not a Java programmer. However recently Netbeans 6, which is still in beta, has been getting some good press for its Ruby on Rails support. So I thought I’d give it a run for a couple weeks.

I’ve been using Netbeans for a little over two weeks now. My verdict: plan and simple Netbeans is kick ass. I’m sold. It may still be a little rough around the edges being a beta and all. However even with that said, it beats any other IDE or editor for Ruby on Rails hands down. Yes in this bloggers opinion(itbo?), this statement includes the beloved Textmate.

I miss my home row. jVi anyone?

I won’t list all the great features as I’d just be repeating what George Cook has already done. One thing George Cook didn’t cover in his article about Netbeans was jVi. Now as a Vim lover myself, jVi just makes Netbeans that much better. With jVi and Netbeans together, I feel right at home.

Netbeans is an awesome editor by itself. However in using it, I soon started to miss my Vim home row goodness. jVi to the rescue, jVi is a Java port of Vim and it works with Netbeans. You can use all the Vim commands while still using all the awesome features of Netbeans: code completion that really works, incredible SVN integration, debugging, documentation, etc, etc.

Try it out

You can get Netbeans 6.0 Beta 1 from here. I’d however recommend using one of the daily builds available.

One thing I should note is, I’ve noticed a issue when accessing certain features a blank or empty window will popup. If you run into this, make sure you are using the latest JDK. As for me it seems like that was the issue, however your mileage may vary. You can change the JDK Netbeans uses by using the ‘—jdkhome’ command line option.

$ bin/nbrubyide --jdkhome /usr/lib/jvm/jdk1.6.0_03/

Once you have Netbeans up and running, jVi is really easy to install. Just download jVi for Netbeans. Inside Netbeans goto Tools->Plugins and then click the Downloaded tab, click “Add Plugins..”, and install.

Documentation and links

Trials and tribulations of class variables in Ruby. 0

Posted by Jason Yates
on Saturday, August 25

I guess I missed the discussion earlier this year about Ruby and class variables. There were a lot of posts about the problems with using class variables incorrectly, many of which, called for the use of class instance variables instead.

I recently fell into this class variable trap described in the posts above. Here is some code simulating my problem.


class Person
  @@count = 0
  def initialize(name)
    @name = name
    @@count += 1
  end
end

class Customer < Person
  @@count = 0
  def self.count
    puts @@count
  end
end

a = Person.new("Jim Bob")
b = Person.new("Jason Yates")
c = Customer.new("Albert Einstein")

puts  "Output: Customer.count"
Output: 3

You would expect to get ‘1′ back from the ‘Customer.count’ method. It is the more obvious behavior or the one with the least surprise. The issue is, as you may have already noticed, class variables are shared with its subclasses. This behavior, speaking from personal experience here, can lead to very confusing and hard to diagnose bugs.

The problem above is easily solved by the use of class instance variables.


class Person
  class << self; attr_accessor :count; end
  def initialize(name)
    @name = name
    self.class.count ||= 0
    self.class.count += 1
  end
end

class Customer < Person
end

a = Person.new("Jim Bob")

b = Person.new("Jason Yates")
c = Customer.new("Albert Einstein")

puts  "Output: Customer.count"
Output: 1

As you can see this simulates the expected behavior.

This will all become mute fairly soon anyways. In Ruby 1.9, class variables are now read-only by its subclasses. With this even the first example should work correctly.

Pimp my gnome-terminal 0

Posted by Jason Yates
on Tuesday, July 31

I saw Rob Orsini pimped his iTerm. I admit, I got a little jealous. What about gnome-terminal? You may ask, do people actually program Rails on Linux and use Gnome at that? Well I atleast know of one guy who does.

I was tired of manually opening Vim, starting the Rails server, tailing the development log, and opening the Rails console every time I opened a project. So I pimped my gnome-terminal.
#!/bin/bash

if [[ $# == 0 ]]; then
  PROJECT_DIR=$PWD
elif [[ $# == 1 &amp;&amp; -d "$1" ]]; then
  PROJECT_DIR="$@" 
else
  print "usage: pimp-term.sh [rails project directory]" 
  return 1
fi

cd $PROJECT_DIR

gvim 

gnome-terminal \
  --tab -t "app" \
  --tab -t "server" -e "sh -x -c './script/server'" \
  --tab -t "console" -e "sh -x -c './script/console'" \
  --tab -t "log" -e "sh -x -c 'tail -f log/development.log'"
$ pimp-term.sh project/myblog

You’ve officially been pimped.

PDF Templates via Rails 0

Posted by Jason Yates
on Monday, March 05

Recently, for a project, I needed a PDF generator which took existing PDF as templates and filled in data. My simple requirement was PDF look and feel wouldn’t require any coding. How hard could that be? Famous last words.

The Problem

The majority of PDF generators out there are exactly that generators. They simply create new PDF’s based on programming code. They don’t edit existing PDF files, let alone have the ability to fill in data. So I looked into a couple other solutions. First I checked out image based solutions like using ImageMagick. I could of course edit existing image files and add data. But, the problem is the added image data would have to be positioned. If the template image changed, so would my positions in code. So that was out.

I tried using PostScript files saved from Adobe Illustrator. I created a template and placed text like <% replace_me >. That should work right? Wrong. For whatever reason Adobe Illustrator likes to create HUGE files. Yes 100,000 lines long in some cases. It also splits up text arbitrarily. So ”< replace_me >” became ”< r” 20 lines of positioning and font information, “eplac” 20 lines of positioning and font information, etc. Making a search and replace impossible.

iText to the Rescue

iText is an excellent Java PDF library. In fact, I believe other then it’s C# counterpart. Is the only PDF library which could do what I wanted. iText can take existing PDF’s and manipulate them. It also can take blank PDF forms and fill out the data, similar to taking HTML form and setting each form field tags value attribute. This is exactly what I wanted. Although it is an ad-hoc round about solution it does work.

Creating PDF forms is pretty easy also. Downside is only one product can create them Adobe LiveCycle Designer, which comes with Adobe Acrobat Professional.

Solution

So first I needed to create a wrapper around iText using the excellent Ruby Java Bridge(Rjb).

require 'rjb'
Rjb::load('lib/itext-1.4.8.jar')

class PDFStamper
  attr_accessor :writer

  def initialize( template = "proposal_template.pdf" )
    filestream   = Rjb::import('java.io.FileOutputStream')
    acrofields   = Rjb::import('com.lowagie.text.pdf.AcroFields')
    pdfreader    = Rjb::import('com.lowagie.text.pdf.PdfReader')
    pdfstamper   = Rjb::import('com.lowagie.text.pdf.PdfStamper')

    reader = pdfreader.new( template )
    @stamp = pdfstamper.new( reader, filestream.new( tmpfile() ) )
    @form = @stamp.getAcroFields()
  end

  def set( key, value )
    @form.setField( key, value.to_s )
  end

  def fill
    @stamp.setFormFlattening(true)
    @stamp.close
  end

  def tmpfile
    return @tmpfile unless @tmpfile.nil?
    @tmpfile = File.join( Dir::tmpdir, make_tmpname )
  end

  private

  def make_tmpname
    return 'proposal-' + rand(10000).to_s + '.pdf'
  end
end

Then using Adobe LiveCycle Designer, I simply created a PDF form and added textfield’s which would be filled out by iText. The textfield’s can be styled, so don’t think you have to keep that normal “textfield” look. Make sure you give each textfield a name that you will use “set( textfield_name, value)” to set the value . In my PDF I simply named each textfield after the database fields. Then in my controller code, I had the following.

def output
  order = Order.find(params[:id])
  pdf = PDFStamper.new

  for column in Order.content_columns
    pdf.set( column.name, order.send(column.name) )
  end

  pdf.fill

  send_data( File.open( pdf.tmpfile ).read,
    :filename =&gt; "order.pdf",
    :type =&gt; "application/pdf",
    :disposition =&gt; "inline" 
  )
end

Caveats

First of, you need Java environment variables set correctly before this will work.

export LD_LIBRARY_PATH=/usr/java/jdk1.6.0/jre/lib/i386/:/usr/java/jdk1.6.0/jre/lib/i386/client/:./
export JAVA_HOME=/usr/java/jdk1.6.0/

You can set these variables in the command line and start mongrel manually “mongrel_rails start”. Which will work fine. Except in production this isn’t really a good solution.

I ended up using the mongrel_cluster init.d script that comes with mongrel. Documentation is available here. I simply placed the export commands on the top of the script.

Another issue I hit was when Java starts. Java will check for total available system memory and then precedes to steal a good portion of it. Now this isn’t a problem with a dedicated server. A virtual server, on the other hand, is allocated a portion of the available system memory. So if the server you are on has 4gbs of memory. Java thinks it has 4gbs to play with, not the 256mb allocated to your virtual server.

This caused this weird issue where one mongrel process in my cluster would work and one wouldn’t. Because each mongrel instance starts its own Java process. The first one would steal all the available memory. Then the second couldn’t even start because no memory was available.

Making matters worse Rails or Mongrel, not sure which, would hide this memory error. I didn’t figure it out until I created a test script that forked, each fork loading the iText jar. The test showed the error coming from Java.

To fix this, I set the _JAVA_OPTIONS environment variable. The options get sent to Java as it loads, it limits the amount of ram each Java instance can eat up. Just place this next to your other Java environment variables inside your init.d mongrel script.

export _JAVA_OPTIONS='-Xms16m -Xmx32m'

You may have to fudge these numbers a little for your particular environment. Or, if you are using a dedicated server don’t worry about it.

Limitations

Now for my particular needs, I only needed text placeholders for the template. However, I believe using LiveCycle designer you can place image placeholders and table based data. Then use iText to fill them in. Don’t take my word for it though.

Who needs compliance, we have "improvements"? 0

Posted by Jason Yates
on Thursday, August 17

Microsoft’s Chris Wilson, the Group Program Manager for IE, was interviewed by ZDNet today and with great marketing finesse managed to completely dodge the whole standards compliance issue. Instead Chris talks about “standard improvements” or “improvements in our standards support in IE7”. I applaud the IE team here for the IE6 bug improvements found in IE7. However, what about actual standards compliance? According to the IEBlog, the IE7 team has added support for.

  • HTML 4.01 ABBR tag
  • Improved (though not yet perfect) <object> fallback
  • CSS 2.1 Selector support (child, adjacent, attribute, first-child etc.)
  • CSS 2.1 Fixed positioning
  • Alpha channel in PNG images
  • Fix :hover on all elements
  • Background-attachment: fixed on all elements not just body

Most of these added features still have issues, or caveats related to them. Slack, of course, should be given with IE7 still being in beta. Overall though even with these additions, standards compliance hasn’t changed much in IE7. Web Devout analysis of both IE6 and IE7 confirms this. First let me say a semi-legitimate argument could be made saying Web Devout’s numbers are biased towards IE. However I still think is fairly effective especially when comparing IE with itself.

  IE 6 IE 7 Firefox 1.5 Opera 8.5 Opera 9
HTML / XHTML 73% 73% 90% 85% 85%
CSS 2.1 51% 55% 93% 92% 95%
CSS 3 changes 10% 13% 27% 8% 22%
DOM 50% 51% 79% 78% 84%
ECMAScript 99% 99% Y Y Y

As you can see, there really isn’t much difference between IE6 and IE7 with regards to standards compliance. Most importantly, in my opinion, IE7 is still lacking:

  • DOM Level 2 Events(Netscape Communicator had this in 2000!)
  • DOM attributes are still broken
  • No Javascript 1.6 support
  • CSS :focus
  • SVG support

However my list is quite short. There are many more lists out better than mine.

I wouldn’t go so far to say as IE7 is “just a bug release”. It is fairly close however. IE7 is still years behind most modern web browsers, and it hasn’t even been released.