Waking up to CoffeeScript

Just clicking around on github today ran into some files with “.coffee” extension. First I thought someone wanted to be funny with the ‘name’ as they might be drinking way too much coffee while sitting in front of a computer, coding. Checking the contents of the file things looked interesting.
I turned to our best friend ‘google’ and found out about coffee script!
I am not sure if I will be using it in any of my project any time soon, but I thought I should just start with a little ‘Hello World’.

You can find more info on their website, http://jashkenas.github.com/coffee-script/

Installing CoffeeScript
I am on Ubuntu, so, here are steps on how to install CoffeeScript on Ubuntu.
At the time of this writing, the latest version of coffeescript is 1.1.2.
CoffeeScript depends on “Node.js”, so we have to install Node.js first.(0.4.12 is the latest version at the time of writing)

Let’s Install the Node.js

$ wget http://nodejs.org/dist/node-v0.4.12.tar.gz
$ tar xzvf node-v0.4.12.tar.gz

At this point you should have nodejs ready to be configured.
Let’s configure it next

$ cd node-v0.4.12/
$ ./configure  --without-ssl
$ make

(Note: I had issues with ssl so I had to configured it without ssl, you may try without the flag)

Congratulations, you have successfully configured node.js!

Now, Lets Move on to CoffeeScript

Download the latest CoffeeScript and set up by following the steps below:

$ wget --no-check-certificate --trust-server-names     http://github.com/jashkenas/coffee-script/tarball/1.1.2

$ tar xzvf 1.1.2

Once you are done with above steps, you should have a directory something like, ‘jashkenas-coffee-script-1a652a9’

Lets take it for a test drive

$ cd jashkenas-coffee-script-1a652a9
$ bin/coffee --version

You should see an output similar to below:
CoffeeScript version 1.1.2

Create a file and call it “test.coffee” (in same location are you at right now)

alert 'Hello World!'

$
Let’s Compile, to compile a coffee script you simply type the following:

$ bin/coffee -c test.coffee 

You should see a file called ‘test.js’ with content

(function() {
  alert('Hello World!');
}).call(this);

We have successfully written a first “CoffeeScript” Code!
There are tons of examples on their site, we can give it a try at some point.

Reference: http://net.tutsplus.com/tutorials/javascript-ajax/rocking-out-with-coffeescript/

Advertisements