« August 2006 | Main | January 2007 »

October 25, 2006

Is Ruby enterprise ready? Agile Web Development with Rails - failed in chapter six

Recently I saw on TV the movie “Contact” with Jodie Foster. The story is about a radio astronomer (Jodie Foster) who discovers an intelligent signal broadcast from deep space. She and her fellow scientists are able to decipher the Message and discover detailed instructions for building a mysterious Machine. (description from IMDB) She explains to the guys from the government and pentagon, that the message is based on primary numbers and contains different mathematical theorems and that mathematics is the universal language, that every advanced civilization can understand. Than the guy from the pentagon asks, if these aliens were so intelligent, why do they not send their message in our language. Jodie Foster explains, that the creatures from the other side of the galaxy speak an other language then english and even on our planet 80 percent of people speak an other language then english.

But what really shocks me, that the guys on loudthinking.com show the same attitude as the pentagon bonehead from the movie. “lots of people are happy with rails and successfully develop software without unicode”…

…or something like this. However I do not remember, if it was DHH personally or if it was in one of the comments. Somebody with similar attitude could answer “a lot of people successfully develop software without ruby and rails and are perfectly happy”. Not me. I thought, I’ll give it a try and go through the “Agile Web Development with Rails”. The book itself is great - suitable for both beginners and experienced proffessionals. The first half is build around sample application and introduces more and more advanced concepts. The second half describes features of rails in depth. There is even appendix with introduction to ruby so you probably do not need separate book about the programming language itself.

The Book

I started with the book on page one and went through the book sequentially - read and tried the code out. I found just copying and pasting from the book not that demanding. So I wanted to implement some different application, but a similar one to that from the book, so I could try all the concepts out. It was great, with rails you can solve typical issues in a web application very easily. Meaningful templates and defaults already solve 70 percent of the problem, you just need to supplement the code here and there to implement your special wishes. It was great, till chapter six… I created web pages for viewing/editing/creating records by using ruby generate scaffold Category Admin, typed in data (partly in russian), went to the listing of the records, and saw only a letter salad, or more exactly a symbol salad with black diamonds with a question mark inside. My first thought was, it is some small configuration problem in browser, I just need to click here and there. After some hours(!) it was clear that Rails and MySql were destroying data I was typing in, and that it was behavior by design. :-(

The idea they promise is that software development should be more straightforward: active record and scaffolding facilitate fast development with little code. The reality is, that you have to search and than go through thousands of howtos and blogs to make rails save and retrieve my name from the database. The same with linux desktop and the promise of the freedom… the freedom to waste nights getting WLAN or {subtitute your hardware component} or DNS, Samba or {substitute your favorite service here} working. ;-)

The Workaround

For the unicode support I did the following:

  1. changed $KCODE in environment.rb
  2. change the application.rb, the mother of all controllers to issue the proper http header “text/html; charset=utf-8”. But it is proper only for html, you should expect problems if you emmit other file types like SVG (graphics) or JavaScript (AJAX)
  3. tried to compile ICU http://www.ibm.com/software/globalization/icu and to use the ruby wrapper library for it
  4. put encoding: utf8 into the database.yml

Eventually it worked. I mean, if I click on edit, than type in the string, save and let the rails show the record again, it shows the same, as I typed in. It is not corrupted anymore. This is a great achievment. The most funny thing is the footer “this worked for me” in the most of the howtos. It means, each of thease howtos definitly works only in the environment of the author. Differences in versions, environment etc. make thease houses of cards falling.

And I am afraid we can not expect any improvement in the near future. The main maintainers do not bother - “works for me”. They are at least very excentric and do what they want, not what others or the majority or “the big enterprise” want to have.

After thinking longer about it, I came to the conclusion, that no addition of unicode support to the existing platform is possible. Either it is unicode based or not. Unicode is not an additional feature or choice, it is the solution for the problem by NOT having the choice. Take Java or .NET, every string variable is unicode string, you do not have any choice, so you can not screw things up. The programming is straight forward. You still have a choice for serializing encoding (the only source for mistakes, and this source is still heavily used) - when you write stuff to your database, to file, or send it to the browser. Always take utf-8 and you and your customers will never have any problems (exception - communication with legacy systems). For our northern american friends it is exactly the same as ASCII, for all others it is enough to represent all they exotic letters (буквы).

Enterprise ready

In my opinion the support for the unicode is a lithmus test for a framework that shows if it is not enterprise ready. I see the following chain: enterprise ready -> should be mature -> used by lot of projects -> projects for globally operating companies -> needs unicode. Today we do not have many computations in our applications, it is more saving, retrieving data and manipulating strings - concatination, accessing substrings, checking length. So proper string support is essential and belongs to the core or the platform.

An other “enterprise”-thing is the size, big companies have a lot of customers/employees/products. So performance matters. So if ten users fully load your server, it is probably not enterprise ready, even if it is designed for scalability like Rails (share nothing pattern). Lets have a look at a simple performance comparision.


Ruby gurus say, a developer should not bother with performance. Developer time and cleaner code are more important than CPU cycles. But the more important is the high performance standard library with powerful and fast string functions.

Also compare, how elegant or less elegant a simple task like time mesurement for computing string length can be solved with particular platform.


require 'benchmark'

# string operation implementation from http://www.ruby-forum.com/topic/69291
def strlength(str)
   n = 0
   str.gsub(/./m) { n += 1; $& }

s =  "Vladimir Добряков"
puts s, strlength(s)
how_many = 100000
elapsed_seconds = Benchmark::realtime do
    i = 1
    while i < how_many
        l = strlength(s)
        i += 1
puts (how_many/elapsed_seconds).to_s() + " operations/sec"
# Result: 22084 operations/sec


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from timeit import Timer
seconds_elapsed = Timer("for i in range(1000):l = len(u'Vladimir Добряков' + str(i))", "").timeit(500)
print 500000/seconds_elapsed, " operations/second"
# Result: 411792 operations/second


public class StringLength {

    public static void main(String[] args) {
    String s = "Hello Владимир";
    long start = System.currentTimeMillis();
    long how_many = 1000000;
    for (int i=0; i<how_many; i++) {
        String s2 = s + i; // avoid caching by using different strings
        int n=s2.length();
        // String strU = s.toUpperCase();
    Long dif = new Long(System.currentTimeMillis()-start);
    System.out.println(dif + "ms");
    System.out.println(how_many*1000/dif + " operations/second");

    // 1127395 operations/second

Visual Basic .NET

Module Module1
    Sub Main()
    Dim s As String = "Vladimir Добряков"
    Console.WriteLine(s & " length=" & s.Length)
    how_many = 1000000
    Dim start_ticks As Long = DateTime.Now.Ticks
    For i = 1 To how_many
        Dim s2 As String = s & i ' avoid any length caching
        l = s2.Length
    Console.WriteLine(how_many / (New TimeSpan(DateTime.Now.Ticks - start_ticks).TotalSeconds) & " operations/second")
    ' 1523809 operations/second
    End Sub
End Module

performance comparision diagram


I think a compromise and not something radical is better in the majority of cases. Take desktops/notebooks. Mac is probably the best. (I do not have any yet, but I am still a big fan of it ;-) because I see how productive people can be with it). It is not from the evil empire and not related to any company with three big letters in the name, but it is not from an anarchists group either.

My favorite is python, something more mature, more main stream than ruby. With lot of great libraries and frameworks for any field. But still concise, powerful and elegant and not bureaucratic and ugly as java or C#. My second choice would be Visual Basic .NET. Visual Basic looks more like python. And .NET brings powerful, extensive API/libraries. So no chance for Wasabi. By the way, if you want your own home grown domain specific language, I think it is better to base it on Ruby than on VBScript. But this is another story…

Posted by VladimirDobriakov at 11:05 AM