I'm all about Ember.js recently

John Doe Is Dull: The Importance of Choosing Good User Names in Tests

We, developers, have to come up with user names all the time. In this glorious, pioneer era of TDD, more than ever we have to not only enter them into text fields but write them in test files that are part of the code and thus are clearly visible and permanent.

So it is important to give good names, but what is a good name like? I am not thinking about the semantics of the name (whether it has accented letters, or if it has two or three parts, etc.) but look at them from a very personal point of view: whether it makes you, the programmer, satisfied with your choice of names or bored with repeating the same old names you used in other projects and even in those projects, you had copied them from somewhere.

So when writing your first test where a user is needed, stop for a moment and imagine a world which you like and which is amusing in the context of your application’s domain (like philosophers playing soccer, Luddites attending a technology conference, etc.). Pick some names and let the feeling that you have created something instead of just using old, boring names fill you with contentment. Whenever you need an other name, take one from your world. If you need further data (e.g date of birth) about your test users you can even do a little bit of research.

Though investing energy in this may reduce the time you spend writing code, compared to all the hours you put into a project, it is negligible. Nurturing this “subculture” in your tests can be very entertaining so you have a good time developing and are less prone to boredom. I think it pays off. We, Ruby programmers all know how important it is to be happy while programming and the effect it has on our productivity.

And besides, do you know of anyone who is called John Doe?

The Hashtag (HRT) Retweet Bot Gemified

A very simple twitter bot that retweeted everything that was tagged by a certain tag was put together for Scotland on Rails ‘09. Then, Jaime Iniesta adapted it for Euruko ‘09. I then forked from his repository, made a few simplifications and minor improvements and DRY-d its configuration.

Maybe most importantly I then turned it into [a gem][hashtag_retweet_home] so that configuration files (including the bot’s which has the credentials for the twitter account) do not have to be included in the repository.

I also set up a twitter account for the budapest.rb group where an instance of the HRT bot will post any tweets that have been tagged with #budapestrb. So, my fellow budapest.rb programmers, if you have anything to say about the budapest.rb, just make sure you include the #budapestrb tag in your tweet and watch the HRT bot repeat it.

To launch your own hashtag retweet bot, just follow the instructions in the README.

Pragmatic Thinking and Learning on Twitter

Some of you may be aware of my grandiose plans with my twitter mini-framework named twuckoo. For my part I am constantly thinking about -hopefully reasonable- ways that it could be used for the better of mankind :)

The other part of the story is my enthusiasm with Pragmatic Programmers books. I have recently started Pragmatic Thinking and Learning by Andy Hunt which is also a very good one and found the familiar “resuming card” at the end. This card sums up the wisdom of the book in short phrases just so that you can recall what you read quickly. It is a great idea and can be found in other PP books, too.

So I asked at a PP email address which seemed to be closest to copyright questions whether I have the right to do this. To my surprise, it was the author himself, Andy Hunt, who answered me and kindly gave me permission for the book in question. (Once again, thank you, Andy!)

So there you have it, a twitter account that will serve you an advice each day just so that you can “refactor your wetware” (the subtitle of the book) and hopefully help you be a better craftsman.

And now, my little twuckoo, on to world domination!

Installing Ruby on a Linode Slice

To be able to experiment freely and find a store for my twuckoos and other goodies I have recently purchased a Linode 360 slice and had Ubuntu 9.04 installed on it. Next thing was setting up ruby (ruby 1.8.6, to be precise).

When deciding about how to install ruby, in part I wanted to have flexibility but mostly I wanted to feel like I am a professional sysadmin for some hours so I balked at pre-built ruby packages and went with the roll-your-own approach. I’ll summarize below the process and some pitfalls I encountered so it might help someone.

Some basic tools

First, you will need wget (or curl) to download the sources you will need along the way, so:

$ sudo apt-get install wget

Also, before you stare dumbly at the screen (like I did) wondering why can’t the stupid machine run make when the Makefile is clearly there in the directory , don’t forget to install make:

$ sudo apt-get install make

Updating the package repos

Plus, since we’ll be installing packages that are found in the “universe” namespace (or whatever it is called) you should uncomment the below two lines in /etc/apt/sources.list :

deb http://us.archive.ubuntu.com/ubuntu/ jaunty universe
deb-src http://us.archive.ubuntu.com/ubuntu/ jaunty universe

… and then update your package index:

$ sudo apt-get update

Installing some packages ruby needs

Before we download and build ruby, however, it is recommendable to have the zlib and openssl libraries so ruby can already bind to them. This is one of the things that is a lot easier to see with hindsight, but here I am, blogging this to you:

$ sudo apt-get install zlib1g zlib1g-dev zlibc
$ sudo apt-get install openssl

It turns out from this thread, that the zlib bindings are part of ruby since 1.8.6 so you will not need the libzlib-ruby package.

Now, on to ruby itself.

Ruby & Rubygems

$ wget ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.6-p369.tar.bz2
(untar and cd to new directory)
$ ./configure
configure: error: no acceptable C compiler found in $PATH

Piece of cake:

$ sudo apt-get install gcc
$ ./configure
configure: error: in `/home/balint/ruby-1.8.6-p369':
configure: error: C compiler cannot create executables
See `config.log' for more details.

A bit harder, but I have quickly found the answer here, and then did:

$ sudo apt-get install libc6-dev g++
$ ./configure
$ make
$ sudo make install
$ ruby -e "p 'hello from linode slice'"
"hello from linode slice"

Rolling!

However, we all know that a ruby installation without gems is a toothless tiger (lion?) so we’ll install rubygems last, first downloading the source, and then installing it:

$ wget http://rubyforge.org/frs/download.php/57643/rubygems-1.3.4.tgz
(untar and cd to new directory)
$ sudo ruby setup.rb

If you want to use gems stored on github (and you probably do) it is convenient to add the gem github repository to your gem sources:

$ gem source -a http://gems.github.com
$ sudo gem install balinterdi-twuckoo

Ok, that’s all. I hope you have found this useful.


Note:

If you receive an error message similar to the following when trying to install a gem:

/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- zlib (LoadError)

Then you need to install the appropriate lib package and recompile ruby. Go to the directory where you unpacked the ruby source to and do:

$ make clean
$ make
$ sudo make install

Twuckoo - My Twitter Mini Framework

I am reading Andy Hunt’s excellent Pragmatic Thinking and Learning book. There is a section about so called “oblique strategies”, short, mysterious phrases that exercise your mind and make you look at problems from another perspective (e.g “repetition is a form of change”).

The suggested intake was once a day and there was a free application that did just this for Apple computers. However, the application was so outdated that I could no longer run it on my laptop. So, I set out to write that application and after a bit of thinking decided to make a twitter account where I (“I” meaning a script that I wrote :) ) would post an oblique strategy each day and then just follow that account. This way, I can also spread the knowledge around.

Thus Twuckoo was born. Twuckoo is a simple program (relying on twibot for communication with twitter) that lets you periodically post a message on a twitter account. It can fetch the messages from a file, from a database, from the web or whereever you want it to retrieve them from. Its very modular in nature so it delegates the task of getting the next message to twitter and storing the posts to the module. here is currently one generic module, the one that fetches them from a file, and one that fetches them from wikipedia’s main page. I strongly hope that it is very easy for you to write your own module for your own purpose. Twuckoo aims to follow the “provide-what’s-needed-and-then-just-get-out-of-the-way” principle. Go check out the README for further details.

It’s quite easy to install and run. You can find an example for the file-fetcher module at http://twitter.com/daily_oblique and http://twitter.com/wikipedia_tfa grabs “Today’s Featured Article” from wikipedia and posts the title and the link so you can grow a little wiser each day :)

Since it is modular it’s very extensible. I kept the API very simple so writing modules should be a piece of cake. Each module has to define the following methods:

  1. load_tweets Loads the messages that can be twittered.
  2. next Gets the next message that will be posted.
  3. store Store the tweet after it had been posted.

Once more, I encourage you to scan through the README, devise your own module and let me know about it. Any feedback is very welcome.

Note: For my Hungarian-speaking readers, I gave a lightning talk about twuckoo at the latest budapest.rb meetup.

Upstream or Downstream?

With the proliferation of middleware applications the mentions of upstream and downstream servers has intensified so I decided to get to the end of this and find out what exactly an upstream server is. I first looked up wikipedia:

In computer networking, upstream server refers to a server that provides service to another server. In other words, upstream server is a server that is located higher in the hierarchy of servers.

Ok, so based on this simplistic definition, a sketch of a request’s route to the application server and the response back can be illustrated like this:

In this drawing, the middleware is an upstream server to the underlying application since it provides a service to it (be it caching, url mapping, exception handling and plenty of others).

So now with my definition established, I was looking for examples in the wild that support it. Jon Crosby’s excellent presentation at the MountainWest Ruby Conference used upstream in this context as far as I remember. Check one.

Browsing the source code of rack-cache -which, by the way, I strongly encourage you to do if you want to understand caching (better) or just like the look of clean Ruby code- I realized Ryan Tomayko’s definition of upstream coincides with the above one since he uses downstream in the code to refer to the component/application below the rack-cache component. Check two.

I recently set up an nginx web server as a front end server to a mongrel cluster that runs a Rails application. Ok, so in this case, the nginx server must be the upstream server since requests hit there first and then are passed to the mongrels. One must hear the sound of collapse of cards in my head when I saw the following nginx directive:

upstream production_mongrels {
  server 127.0.0.1:8000;
  server 127.0.0.1:8001;
  server 127.0.0.1:8002;
}

Hmm, what? Nginx is above the component it provides a load-balancing service to so by any -ok, by my- definition the directive should say “downstream” and not “upstream”.

I realize that the request arrow on the diagram moving from top to bottom is a matter of convention and that it probably correlates with the fact that most languages write from the top to the bottom. Nevertheless, as far as I know Nginx is made by a Russian guy and Russians write top to bottom, too, so he should still use “downstream” for the directive, now shouldn’t he? Or did he simply start to sketch up boxes and arrows from bottom to top for the sake of it when designing his web server?

Summing up, I think the above described convention is the common one but since it is not standardized there will always be people and software that uses it the other way around. I am not overly confident though, so please tell me about your definition or just point to examples which support or contradict mine.

Finding Missing I18n Translations With Missing_t

A recurring problem in an internationalized Rails project is to see which translations are missing. You could go through all the pages looking for “translation missing: en, user, new” texts. That, however is very tedious and error prone. There may be some texts you don’t even see that are missing translation. (like a title of an image).

A better solution is to write a program that finds these. So that’s what I did. I gave it a very imaginative name: missing_t. It’s darn simple. You first have to install the gem:

gem install balinterdi-missing_t  

and then from the root of your Rails app you type:

missing_t

You will see all the I18n strings that do not have translations along with the file they were found in. If you want to restrict your search for a specific language, you provide its language code as the first parameter, like so:

missing_t fr

For a bit more detailed description, see the README.

Go ahead, give it a try, your app has the right to speak all those beautiful languages out there!

Thrifty - Track Your Expenses Easily

Thrifty is a simple application I have written (and am still writing) in Sinatra that tracks your expenses. You can quickly record each spending you make and assign tags to them so you can group your expenditures easily. You can then see how much you spend in different categories (by tags) possibly narrowed down to a given date interval.

Anyway, a web app is all about interaction so give it a try on the test site or download it from its github repo and run it locally. You’ll also find a somewhat detailed README there.

This is a very early release so I’d appreciate any kind of feedback, be it a bug report, a comment on bad UI or anything you might find worthwhile sharing.

Plans for 2009 - Contribute (More) to Open Source Projects

At the start of 2009 my friend Jaime resumed his previous year and proposed me to do the same. It seemed a really good idea but somehow it still died down.

I know it’s March now but I reckon it’s still ok to do this until December :) I’ll attempt to write a mini-series of posts about not so much what I did in 2008 but rather what I would like to achieve in 2009.

When I started to work full-time with Ruby I also realized the vast possibilities and magnetism of open source projects. I could -and still can- spend hours browsing among different projects on github, peeping into their code, reading related articles, etc.

So when I set out for this journey one of my tasks naturally became to regularly contribute to a “serious” open source project. When I got acquainted with Merb I also got to know DataMapper, its default ORM. I admired its elegance and coherence and the more I knew of it the more I liked it. So when the maintainer position of a dm-more gem, dm-constraints* seemed to be up for grabs I took the opportunity.

That was in December and since then I sent in a few patches and merged changes implemented by others. (which I realized could sometimes be a lot more difficult than writing code :) ). I like to do this and have learned -and probably will learn- a lot so one of my plans for 2009 is to continue with dm-constraints and maybe find other open source projects I could contribute to on a regular basis.

So, if you happen to use DataMapper, which I strongly encourage you to do, and find a bug related to foreign-key constraints, file a ticket and assign it directly to me (Balint Erdi). And/or start contributing!

Notes

* Dm-more is the meta package that contains everything outside the core functions of DataMapper. Dm-constraints deals with foreign key constraints. If you are familiar with ActiveRecord, it is basically the options you specify with :dependent on an association.

Easily Quote Passage With Mail in a Reply

That was too long to twitter, so here it goes.

If you use Mail (OS-X) when replying to a message you can select some text in the email you want to reply to and then hit reply. The new email will only include the text you selected so you don’t have to delete all the lines you don’t need. I hope someone finds this useful, I really do.