EAN UPC Support

EAN/UPC/GTIN calculations for Ruby

To install the Ruby Gem

sudo gem install ean

What can it do?

– Validate UPC/EAN/GTIN identifiers

– Generate check number for UPC/EAN/GTIN identifiers

How to use

Load the gem:

require "rubygems"
require "ean"

Convert to a 14 digit GTIN:


Check whether a number is a valid 8, 12, 13, 14 EAN/UPC/GTIN number


You can also do


Given a 7, 11, 12, of 13 digits number, generates the check digit (to be added at the end.


Adding CodeHighlighter to Typo

Recently, I ran across CodeHighlighter by Dan Webb. This looked like a pretty cool javascript, and I thought a better way than to do it on the server side.

Adding it to typo is fairly straightforward, baring a small patch.

Get the code

Get it from Dan’s site via subversion: http://svn.danwebb.net/external/CodeHighlighter/trunk

svn co http://svn.danwebb.net/external/CodeHighlighter/trunk

Adding CodeHightlighter to your template

Add to your template (themes/[YOUR THEME]/layouts/default.rhtml)

<%= javascript_include_tag "code_highlighter" %>
<%= javascript_include_tag "javascript" %>
<%= javascript_include_tag "css" %>
<%= javascript_include_tag "html" %>
<%= javascript_include_tag "ruby" %>

The code as provided by Dan has a small incompatibility with prototype used by Ruby on Rails. The following line

for (var i in this.styleSets) highlightCode(this.styleSets[i]);

behaves quite strangely and will pretty much kill your browser (Firefox runs out of memory and displays a bunch of “undefined” after several minutes) because it ends up calling hightlightCode way too many time because of all the methods added to all objects.

To make codehighlighter.js play nicely with prototype.js, replace that last line with:

if (this.styleSets.each) {
} else {
    for (var i in this.styleSets) {

Essentially, if prototype is present, we can use each to iterate on the styles, otherwise it is safe to used the old code.

Using Codehightlighter

Now, addng styling is easy. Using Textile, you can just put your code around a <pre> and a <code> block. If you have a javascript snippet, use a class of “javascript”.

For example:

<pre><code class="javascript">
if (this.styleSets.each) {
} else {
    for (var i in this.styleSets) {

The other available styles are “html”, “css” and “ruby”.

For example, here’s how ruby would look like:

  def display_article(article = nil)
      @article      = block_given? ? yield : article
      @comment      = Comment.new
      @page_title   = @article.title
      auto_discovery_feed :type => 'article', :id => @article.id
      render :action => 'read'
    rescue ActiveRecord::RecordNotFound, NoMethodError => e
      error("Post not found...")

And it looks easy to create other styles. See the stylesetguide.html file for details.

Update: Turns out Dan already had a fix, so if you grab the trunk, you will be fine.

Here’s his version of the patch:

    for (var i=0; i < this.styleSets.length; i++) {

Install your own ruby on a shared host

Since I upgraded to Typo 4.0, and in the process rails 1.1.6 I have had a few occurrences where nanoRAILS would hang, several bloated processes would be sitting there and not respond, and the only option at that point was to kill all ruby processes once I realized what was happening, which could be several hours. Suffice it to say, this is not a good option.

So after struggling during last rails upgrade to 1.1 on my host, the next logical step is to also use my own version of ruby so I can have better control on its environment, and even apply patches if necessary.

The following steps apply on a lot of systems. More specifically, my host is DreamHost (aff), and as best I can tell, I’m on a host with Debian Sarge.

Build your own Ruby

Download ruby from http://www.ruby-lang.org. The latest version is currently ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.4.tar.gz

Create the makefile using

 ./configure prefix=[YOUR_OWN_RUBY_PREFIX]

Since you most likely don’t have root access, you need to override where ruby think it resides, and the way to do that is to set the prefix to somewhere into your home directory. Something like /home/USERNAME/ruby for example. From that point on, libraries, other builtin ruby files, gems will automatically install into your own ruby repository so you never have to worry about getting in trouble with an unforeseen upgrade.

Optionally, you can apply the patch used by Railsbench, with hardcoded default values because I haven’t figured how to set the environment variables for the dispatch.fcgi process (since apache in my case determines that). Download my version of rubygc.patch .

 patch gc.c rubygc.patch

Build and install ruby

 make install

Additionally, so that the command line uses the same version of ruby, add this to your .bashrc or equivalent for your shel.


Install your own gems

Now you are ready to install your own gems. Here’s the bare minimum you need.

First, install rubygems

 wget http://rubyforge.org/frs/download.php/11289/rubygems-0.9.0.tgz
 tar xzvf rubygems-0.9.0.tgz
 cd rubygems-0.9.0
 ruby setup.rb

Then install the minimum set of gems:

 gem install mysql
 gem install fcgi
 gem install rails --include-dependencies

Now, the only thing you need is to change the path to ruby in your dispatch file (dispatch.rb for mod_cgi, dispatch.cgi for regular cgi, and dispatch.fcgi for FastCGI/fcgid)

Typically, replace

#!/usr/bin/env ruby



Replace [YOUR_OWN_RUBY_PREFIX] by your own value you used earlier.

textmate rails cheat sheets

I’m just discovering TextMate and I have to say I’m impressed! For those of you who don’t know me, it takes quite a bit for me to admit that I’m impressed.

Over the years, I’ve used quite a few editors, and I happen to think that vi is one the greatest. Hmmm, I can see some eyebrows perking up, and most of you starting to think that I’m weird, either because you’ve never heard of vi, or because you’ve learned to hate it. But I like it because it is dead simple (once you are over the initial learning curve), has powerful regexp, and gets the job done. And once you’ve learned it, that is the kind of thing you don’t forget. I’ve liked emacs at some point, but it is way too heavy, and after a few years of not using it, you’ve got to learn it all over again, but I digress…

Back to TextMate. I’ve not used it much yet, but I love the concepts. Lots of keyboard action (the mouse is great, but going back and forth between mouse and keyboard can be a drag (pun intended!)). And it has very powerful macros that look easy to customize. Give me more!

So I went out I looked for quick ways to get started. I found a couple of interesting cheat sheet that I’d like to share with you. If you know of any other, please let me know!

The first rails textmate cheat sheet is by Sebastien Friedrich and documents the snippets (a.k.a. tab triggers) from the TexMate Rails Bundle (by syncPEOPLE. (via O’Reilly Ruby)

The second is TextMate Cheat Sheet for Rails Hackers is provided by Pragmatic Studio and includes some of the same information, plus some more general textmate shortcuts useful with Rails (via Tim Kuntz).

The third is TEXTMATE cheat sheet and is a more general purpose guide to the shortcuts of TextMate. (via macromates).

And while I’m on the subject of Cheat Sheet, here are 2 bonus rails and ruby Cheat Sheets:

With all 5, you should be all set!

Arghhhhh! The path to ruby lib is wrong!

nanoRAILS was down for the past 12h because of a setup change on my host on DreamHost.

Suddenly, the link /usr/local/lib/ruby was changed to point to an incorrect location, so instead of having the ruby libraries under /usr/local/lib/ruby/1.8, they would effectively be under /usr/local/lib/ruby/ruby/1.8 :(

Suffice it to say that things don’t work too well after that.

Running dispatch.fgci by hand revealed that “require pathname” fails. Hmmm, that’s not supposed to happen! That when I realized the above mentioned link had been changed just a few hours before.

After tinkering some more, and trying a few options, I managed to put a kludge together till the link is put back in the right place.

In the public directory, I created a myruby script:

export RUBYLIB=/usr/local/lib/ruby/ruby/1.8/:/usr/local/lib/ruby/ruby/1.8/i386-linux/
/usr/bin/ruby $*

Make sure that script is executable (chmod +x myruby)

then in dispatch.fcgi, I replaced the first line with:

#!/usr/bin/env [PATH TO YOUR RAILS APP]/public/myruby

Of course, this will likely break once the link is restored to its correct value.

You’ve got to love using a shared host!

Update: ruby setup is back to normal now. At least I learned something in the process.
Everyone that was inconvenienced by this outage, please accept my apologies.

The train is leaving the station!

Let’s get this train on the rails and off to some new and exciting adventures. I discovered rails a month ago and I have been dabbling in it ever since. I had been exposed to ruby a while back, but never got interested, and that’s a shame. Now I’m a few trains behind the curve.

This will be a chronicle of my progress, I’ll highlight what I found interesting, post things that I believe could benefit others (See the Quid Pro Quo Section).

Join along for the ride!


PS: Note to self: we’ll see how long that train analogy sticks around ;)