I'm all about Ember.js recently

2010 Is the Year of the Javascript

If you’ve read the The Pragmatic Progammer - From Journeyman to Master (and if you have not, believe me, you should) you might remember one precious advice: learn a new programming language every year. So when the year draws close to its end (around April, that is) I really start thinking about which language should be the one for me next year.

This reflection is very pleasant; it is such a joy to immerse oneself into a new language, to slowly discover its structure, its subtleties and learn new ways of thinking, of approaching a problem (because that’s probably the biggest gain of it, even surpassing adding another language to your arsenal) that its prospect is already rewarding in itself.

My choice in 2009 fell on Clojure. I love functional programming, its elegance, its cleanness, its statelessness and after getting my feet wet with ANSI Common LISP a while ago I felt like Clojure would propel me on my way of becoming an FP guru. Guided by a great book I wrote a couple of, admittedly not too difficult exercises (hey, a sudoku solver is already something, isn’t it?) and I would certainly like to continue to do more with it in 2010.

Now, on to 2010.

Thomas Fuchs is certainly right when he predicts that Javascript will play an increasingly significant role in web development. I was definitely wrong when I had thought of JS as an undebuggable, structure- and featureless, incoherent language. Of course, you can write Javascript code that is like this but I now think that -at least in the case of Javascript- this tells more about the craftsman than about the language.

Make no mistakes, I can already do Javascript! I can miraculously make a check box appear if you click on a link, or update a database record without reloading the page when you press a button and a ton more such wizardry, really. However, I always felt like there was a whole lot more to Javascript than putting some dumb-looking functions in a file and then include it from my html (no, I don’t think inlining it makes a difference) and I felt uneasy about my javascript functions just standing there on their own without any apparent belonging to a group or some kind of cohesive force or idea that holds them together.

My recent enthusiasm comes from the fact that I had to browse through some of the source code of scriptaculous and I really liked what I saw. The enthusiasm is not totally new but looking at that source code demolished the last shreds of my Javascript skepticism, so to speak. So a year later than Peter Szinek I am hopping on the same train: Javascript, here I come! If you have read a good book that enhanced your Javascript foo, please tell me about it in the comments.

And you? Have you already chosen your language for 2010?

Twuckoo 0.3.5 - With Email Notification

As some of you might know, I am running a few twitter channels that use my brainchild, twuckoo, to periodically send a tweet.

In two of these cases the program takes its next message from a file and takes care not to tweet the same thing twice by keeping track of the messages already sent. This means, however, that the pool of sendable messages are depleted after a while and the script simply does not send any more tweets, the channel becomes dead.

This has happened a couple of times in the past and I only realized it with a few days of delay which is kind of awkward (if you were subscribed to one of these accounts, I apologize). So I decided to have twuckoo send a notification in this case so I can go in and refill its queue. Hence twuckoo version 0.3.5 was born which sends an email to the address set in the configuration file when it failed to send a message.

To install it, add gemcutter to the list of gem sources if you haven’t already:

install.sh
1
gem source --add http://gemcutter.org

And install twuckoo:

install.sh
1
gem install twuckoo

You might be thinking that it would be more straightforward to have the program replenish itself so that no intervention from the part of the developer is needed. And you would be right, this could be a task for the next version. I still think it is a good idea to send a notification so the (wo)man in charge knows it happened.

So go ahead, have a look at the README, set up your own twitter channel and don’t fret about when the queue would run dry. You’ll be notified.

My Tech Book List for 2009

The title probably tells it all, below is the list of technical books I read in 2009. I am -unfortunately :)- not getting paid for any recommendation that lies herein.

My Job Went to India by Chad Fowler

This one is a real motivation booster. Or it might function like love in the case of separated lovers: if you were motivated in the first place, you’ll get a kick out of this book and want to do everything it recommends right from the next day. If you were not it might depress you to have to do so many things to stay afloat. Full of useful advices mostly drawn from the author’s personal experience in India. 5/5!

(There is a second, reworked version under the name: The Passionate Programmer, you should probably buy that one.)

User Interface Design for Programmers by Joel Spolsky

I real like Joel Spolsky’s essays both for the content and for his witty style. I laughed out loud on several remarks in this book, too. The book reaffirms how important good UI is, makes it accessible for programmers by giving guidelines and provides some examples of good (and especially bad) examples. Its style makes the “dry” technical stuff fun to read.

The Pomodoro Technique by Francesco Cirillo

A time-management and productivity tool, the Pomodoro Technique is the latest craze. The technique can be summarized in a few paragraphs, the details fill about 40 pages which comprise this book. The e-book can be downloaded for free. Although I do not follow all the advices described in the book fully (like administering the tasks you have done at the end of each day) I think the technique is a great way to achieve focus especially if you have problems with it. (The PragProgs have also recently published a book about it)

Programming Clojure by Stuart Halloway

Clojure is a functional programming language that runs in the JVM (hence the “j”, I guess). This excellent book contains everything you need to know to start (and continue) programming in Clojure from multimethods to infinite sequences, from ways to deal with concurrency to understanding and writing macros. Clojure rocks and so does this book!

The Principles of Beautiful Web Design by Jason Beaird

I bought this book to be able to do a fancier design than putting black text on white background and using ugly textboxes with Times Roman fonts. It has certainly lived up to this goal and it has some very practical advices and links to resources (I particularly liked the chapter about color). Just as its title says, it explains principles rather than implementation details, which is what I was looking for. That said, I still feel I need another book (and a decent amount of practice, of course) to beam me to the level I had planned to reach before buying it. Any suggestions?

Test-Driven Development By Example by Kent Beck

A true classic, I reckon this book is a must-read for anyone aspiring to adhere to TDD or in fact for anyone already doing it at any level of mastery. In the first two chapters two distinct functionalities are developed (the first one, the Money example, in Java, the second, the xUnit example, in Python) by tiny steps using TDD, at each step pondering on the problem at hand and giving an explanation of the chosen solution. The third and final chapter deals with TDD patterns and is an invaluable addition to the first two chapters. Now that I am writing about it I feel like I should reread this book in 2010 (and then probably once every year). Red, green, refactor!

Pragmatic Thinking and Learning by Andy Hunt

The fact that I have not finished only goes to show its splendidness, I simply don’t want it to end yet. (I read this in a Paul Graham essay but I can’t find it right now). Its subtitle, “Refactor your wetware”, is telling: it shows you how to make yourself more productive by harnessing the innate capabilities of your brain and debugging its intrinsic “bugs”. I guess it is also a very refreshing read because it is about our mind and its workings and not some dry technical stuff. The stories and anecdotes linked to some of the sections makes it easy to recall the message. In fact, I liked this book so much I created a “twitter mini-framework” and a twitter channel for the concise version of the advices.

Life is not just about stubbing, view helpers and achromatic color schemes

On a Christmas evening, I sat down on a comfy sofa with Révolutions by J.M.G. Le Clézio in my hand and was totally immersed in it right from page one. I realized I need occasions like this to repeatedly remind myself how much I miss of life if I only ever read technical books. (Sadly, my list of novels I read last year is a lot shorter than the above one but I heartily recommend you A Thousand Splendid Suns by Khaled Hosseini and Poisson d’or by J.M.G. Le Clézio)

Any good books you have read?

I am also making a (so far, only mental) list of books for this year and would like to hear your opinion, too. So have you read any books lately that you would passionately recommend for others?

I15r Handles Non-english Source

The other day thinking about how i15r could be improved I realized that there are times when the language of the non-internationalized site is not English (surprise, surprise). Add to this that lots of languages have “special” characters, characters that may not be properly found and replaced by the matchers. So I first learned the basics of how Ruby handles encodings (from James Edward Gray’s excellent series) and then adjusted a few matchers to catch those special characters, too. (Note: one can’t underestimate the utility of a spec suite that covers most of the code)

The specs pass but I am pretty certain there are some cases where the matchers might not be up to the task. If you come through such a case, please submit a bug report. Since there has been a huge improvement in string encoding in Ruby 1.9 (basically, strings now have an encoding as opposed to in Ruby 1.8), there might be some cases where strings are properly internationalized using Ruby 1.9 but not with 1.8.

So if you think this is a valuable addition or just have not tried i15r yet, go get it:

install.sh
1
gem install i15r --source http://gemcutter.org

New Year, New Blog

I guess it all began with this blog post. I saw that there is another way for composing a blog post than typing it into a textarea with a WYSIWYG interface (which is, by the way, never really WYSIWYG). It also seemed a lot cooler, something a “real hacker” would prefer hands down. The decision to switch my blog over to a textarea-free motor has been subconsciously made the time I read that post but the implementation needed a year to be realized. I reckon the final push was to see how easy the migration of the blog’s content is.

The tools

I decided to use Jekyll, a static-site generator since it integrates nicely with Github and has all the features I need. Being a static-site generator I needed a client-side solution to handle comments. I chose Disqus because that’s the one I knew and liked but there are alternatives.

The process

Content migration

I needed continuity and thus migrating the old posts and comments. Fortunately Jekyll includes migration scripts for several blog motors, among them Wordpress, so that was a piece of cake, everything worked out of the box.

For the comments, I used a modified version of Lackac’s script, that he so kindly directed my attention to (thank you, Lackac). Clearly it was more work than migrating the posts but nothing to shy away from. (If you plan to switch from Wordpress to Disqus I believe you’ll be able to use the script with a few minor modifications.)

New design

For my Wordpress blog, I used a design template that looked ok but was in PHP and was a PITA to modify or to add some styling to. Since I am looking to improve my capabilities as a designer I thought designing my own blog would be a suitable task to get my hands dirty with. I am not concealing the fact that I borrowed ideas from several blogs I liked but hey, they say it’s not a shame to copy good ideas so what’s wrong with that?

Layout, site feed, etc. with Jekyll

Have I said I love open source? There is a Jekyll wiki that lists Jekyll sites along with a link to their sources so seeing how to set up my blog with Jekyll was quite straightforward. I guess this was still the most time-consuming part of all but I did not really have to think of anything, the steps are well-defined. (Here is the source of my blog if you’d like to take a glimpse.)

Buying a Github plan to be able to use a non-github domain

If you’d like to host your blog under a custom domain (http://codigoergosum.com instead of http://balinterdi.github.com) you’ll need to buy a Github plan. Starting at 7$/month for the Micro plan it’s not an insurmountable obstacle, in fact it costs less than the Wordpress hosting I am moving away from.

Changing the name

Back in the spring of 2008, returning from the European Ruby conference (and having finished The Pragmatic Programmer on the train) my mind was buzzing with new ideas and I could not wait to set myself on a craftsman’s journey.

I felt I needed a blog immediately. The hard part of course was coming up with a name. Since I felt it was more important to get something going than to idle on finding the perfect one (I guess we can call this agile, can’t we? :)), I launched http://bucionrails.com. During the almost two years that have passed since that day I have grown somewhat uncomfortable with that name so I decided to change it. I hope you like the new one :). (I set up a feed redirection with Feedburner to notify you of the feed URL change after 15 days but you could directly subscribe to the new feed now)

The end result

It felt appropriate to start the new year with a new blog and -to my surprise- the switch was a lot easier than I had thought. I hope you, the faithful readers of my blog, welcome the change and ameliorate it with your precious comments.

Now I am blogging like a hacker, too!

I15r 0.3.1 Is Out - Now With Haml Matchers!

I’ve just released a new version of the i15r gem which alleviates the tedious task of internationalizing a Rails site by finding the “static” texts and replacing them with I18n message strings.

The good news -and probably the reason I am writing a blog post about it- is that it can now process haml files, too. This must cheer up those who -like me- have come to like haml so much they now use it for their projects. So go get it and let me know how it works for you.

Rubygems Under the Hood: Hooking Into Require

(See the introduction to the series.)

When you want to pull in a Ruby library, you require it. The library you require has to be on Ruby’s load path ($LOAD_PATH) in order to succeed. So let’s see how Rubygems hooks into this process.

The secret lies in custom_require.rb. You’ll see a nice comment near the top.

When you call “require ‘x’”, this is what happens: * If the file can be loaded from the existing Ruby loadpath, it is. * Otherwise, installed gems are searched for a file that matches. If it’s found in gem ‘y’, that gem is activated (added to the loadpath).

In fact, that explanation looks so straightforward to me that I doubt if I can add more words to precise it, so let’s look at the code together:

module Kernel
  alias gem_original_require require
  (...)
  def require(path) # :doc:
    gem_original_require path
  rescue LoadError => load_error
    if load_error.message =~ /#{Regexp.escape path}\z/ and
       spec = Gem.searcher.find(path) then
      Gem.activate(spec.name, "= #{spec.version}")
      gem_original_require path
    else
      raise load_error
    end
  end
(...)
end

Aliasing a method, redefining it and calling the original version from the redefined method is a very familiar pattern if you have read Rails source code (called method chaining).

So, just as the above description says, if the file can be loaded “without Rubygems” it is and Rubygems never comes into the picture. If not, we make sure that the load error comes from the file not found on the load path and then find the gemspec for that file and activate it.

Activating it adds it to the load path, so we can call the original require safe in the knowledge that it will now succeed and we won’t get another load error this time around (and go into an infinite loop). Clever, heh?

This is one of the cases when the code is so simple for a moment I think I could have written it myself. With its <10 lines of code Rubygems is empowered to load a myriad of useful Ruby libraries. Isn’t this wonderful?

RubyGems Under the Hood: Introduction

I am not sure about you, but one thing I keep bumping into while developing ruby applications is rubygem version conflicts. If you have ever come across “can’t activate activesupport (= 2.3.4, runtime) for [], already activated activesupport-2.3.3 for []” type of errors, you might be sympathetic to the cause.

Since rubygems was and is being written by some excellent Ruby minds I knew the fault probably lies with me and not with the tool. Being a motivated and proud craftsman I felt like I needed to know my tools and rubygems is probably the number one tool most of us Ruby developers use on a daily basis.

I intend to write a mini-series of Rubygems posts. A post will be added to the series when I explore some murky corner of rubygems inner mechanics. Some of the time this will come from a particular problem I come across while coding while on other occasions I might just look into the source code and try to understand what a particular piece of code does.

Now, I realized that when I read on a blog: “I will go into more detail about X in a forthcoming post”, it is the surest sign that you will never read about X again. At least not on that very blog. And I am not saying this in a mean way, I think it is very natural for our plans to change (“evolve” may sound better) even on the short term. So I am aiming low and hoping to give you more than you expected. (Notice I wrote “mini-series” which may just mean a couple of posts.)

Do not set your expectations high

I am absolutely not a Rubygems expert so please do not be fooled by the “under the hood” title. This will not be commensurate to Jamis Buck’s series on Rails routing. I do not -yet?- have a deep insight and I am not even a contributor to Rubygems.

The forthcoming :) series is just a humble attempt to gain an understanding of a very important part of the Ruby arsenal. I hope to understand it better by explaining to you how it works and I hope you will understand it better by reading my explanation. And last, but not least, I hope we’ll both spend less time debugging those tricky issues.

Having said that, I encourage you to participate in the series. Tell me about a particular problem you had with Rubygems. Correct me if I am wrong. Point me to an article where all this is already explained, and much better. Give feedback so this might evolve to be a discussion rather than an academic lecture. Thank you.

Cucumber Meets Selenium Meets Integrity

Continuous Integration (CI) is a basic building block of any project done in TDD style. In brief, having a CI server properly set up guarantees that developers can rest assured that the application passes all its tests and can thus be deployed at any moment. It does this by sending some kind of a warning if something is broken so it can be fixed immediately.

Now I noticed that if a CI server is not in the mix right from the beginning of project, chances are it will never be, that was one of the first things I installed on a new Rails project. My server of choice is Integrity mostly because it is so easy to set up and quite straightforward to use.

Ok, so next we need some tests that Integrity will run at each commit and make sure the app can still be built. As a high-level acceptance test framework, I use Cucumber, which plays very nicely with Selenium for automated in-browser testing. Since nowadays even the most basic web application will have some amount of client-side scripting code (that is, javascript) if you really want to test the features of your application you’ll need Selenium tests.

That’s when matters get a bit more complicated when it comes to integrating these with the Integrity server. Why? Because on the server you usually don’t have the “desktop environment” which is available on the machine you do the development on. By default, you don’t run a desktop manager and have a graphic display on a server. A tool called xvfb comes into a picture that emulate a dumb framebuffer so you can still programs that need a graphic display.

To have some practical guidance, here’s what I did on an Ubuntu server to enable all of the above:

Install java since Selenium runs in the JVM:

apt-get install java-common sun-java6-jre

Install firefox since that’s what Selenium runs by default and that’s a very good choice.

apt-get install apt-get install firefox

Install X since that’s what the xvfb launches and Xvfb itself.

apt-get install xorg xserver-xorg
apt-get install xvfb

Next I found a wiki that describes how to launch the Xvfb correctly. Log into the server and do:

startx -- `which Xvfb` :1 -screen 0 1024x768x24 2>&1 >/dev/null &

So Xvfb will run on the DISPLAY :1. So far so good. But something was still not quite right. When integrity launched the test suite that included some Cucumber-Selenium tests I received an error message basically saying that no browser sessions could be started. And the solution to that, in fact, is where this post wants to get at.

After a decent amount of head-scratching and code mining I realized that the Selenium server starts the browser on the same display where the server itself (the jar file) runs. I have found the relevant code that assembles the command that starts the Selenium server in the selenium-client gem and figured it was not meant to be run in graphic hardware-less environment since I saw no options to define which display it should run on. So as an easy hack I added the hardcoded “DISPLAY=:1” before it and crossed my fingers.

Bingo, it worked and I had a green build again! What’s more surprising is that it still runs perfectly on my Macbook so it seems to run in (some) graphical environments, too. It seems a bit strange to me to have found so little information on this subject since Rails, Cucumber, Selenium and CI are all en vogue so it is possible that I missed something obvious and there is an easier way to do all this. I am very eager to hear how others set up their CI to run automated-browser features. Do you use another tool, not Selenium? Do you use a CI server with a monitor?

Anyway, I certainly hope my hackish solution will prove to be useful for some of you TDD-minded developers out there who run their CI on a simple <name of your favorite provider here> slice.

UPDATE: As a fellow developer and the author of the selenium-client gem himself pointed out library code is not the place for enviroment specific settings. Rather, it should go into the application’s code. I guess that leaves the choice of putting it into the code of the application you are building or the configuration of the CI server. This latter seemed more clean to me so I put the following line into config.ru in Integrity’s directory:

env["DISPLAY"] = ":1"

There is still something left, though. When integrity -or, to be precise, the selenium process that was launched from integrity- wants to access display :1, it will be denied. You need to explicitly enable local connections by putting “localhost” in a file:

echo 'localhost' >/etc/X99.cfg

, and then using that file as the access records list when you launch the server:

xinit -- `which Xvfb` :1 -screen 0 1024x768x24 -auth /etc/X99.cfg 2>&1 >/dev/null &

(Note that I launch xinit and not startx as before. startx somehow adds another -auth option which messes things up)

There, that should do it. It works and there is no code where it does not belong.

A Prime Example of Not Eating Your Own Dog Food

I realized how 20th century (plus boring and error-prone) it is to write one’s invoices by hand. So I went looking for an invoicing application. My expectations were quite low: I wanted one which can be used from my MacBook, has some basic functions that cut down on typing (e.g a client “database”), has a decent design and UI, can make out invoices in different currencies and did not cost more than ~$100.

It seemed like I have found one, which, though it had several minor bugs, seemed acceptable, so I purchased the Basic version. Everything went reasonably well until I had to make out my first invoice in euros. It turned out that the Basic version can only make invoices in Hungarian forints in spite of having a couple of other functionalities (e.g set the VAT rate to 0% automatically on invoices made out to EU companies) which made me think it could make it in euros, too.

Ok, so accounting hell got me again. I sighed and asked the company how much it would cost to have this funcionality. I was informed I had to buy the upgrade to the Business version which costs ~$120 more, roughly the double of the original price I paid! That was too much to take, so I went looking for another invoicing application. After all, it should not be an insurmountable task to write such an app, I murmured to myself.

I did find a couple of very good applications which even overdo what I want them to accomplish. However, the Hungarian homologue of the IRS basically shuts out any foreign competitors by regulating very heavily which conditions must be fulfilled by an invoicing application for a Hungarian company to be able to use it. Bummer.

“There is still a glimmer of hope.” - I thought. I checked the Hungarian scene still thinking it could not be that difficult to meet my expectations. Well, apparently I was wrong. The ones I found are either overcomplicated to use, look like home pages in the 80s or are way too expensive. Sometimes all of these. So I put off the challenge of finding an acceptable piece of software and got back of writing invoices manually.

Now you might wonder where the dog food is in the story. Well, today I received the invoice from the company of my purchase of the Basic version of the application. It was written manually on an invoice sheet :) That’s tantamount of Steve Jobs using a PC with Windows Vista.