index.md (3284B)
1 --- 2 title: "CoffeeScript for Ruby Friends" 3 date: 2010-08-06T00:00:00+00:00 4 draft: false 5 canonical_url: https://www.viget.com/articles/coffeescript-for-ruby-friends/ 6 --- 7 8 Hello there, Ruby friend. You've perhaps heard of 9 [CoffeeScript](https://jashkenas.github.com/coffee-script/), 10 "JavaScript's less ostentatious kid brother," but it might *as yet* be 11 unclear why you'd want to stray from Ruby's loving embrace. Well, 12 friend, I've been playing with it off-and-on for the past few months, 13 and I've come to the following conclusion: **CoffeeScript combines the 14 simplicity of Javascript with the elegance of Ruby.** 15 16 ## Syntax 17 18 Despite its compactness as a language, Javascript has always felt a bit 19 noisy to me. Its excessive punctuation is pretty much the only thing it 20 has in common with its namesake. CoffeeScript borrows from the syntaxes 21 of Ruby and Python to create a sort of minimalist Javascript. From 22 Python, we get significant whitespace and list comprehensions. 23 24 Otherwise, it's all Ruby: semicolons and parentheses around function 25 arguments are entirely optional. Like Ruby's `||=`, conditional 26 assignment is handled with `?=`. Conditionals can be inlined 27 (`something if something_else`). And every statement has an implicit 28 value, so `return` is unnecessary. 29 30 ## Functions 31 32 Both Javascript and Ruby support functional programming. Ruby offers 33 numerous language features to make functional programming as concise as 34 possible, the drawback being the sheer number of ways to define a 35 function: at least six, by my count (`def`, `do/end`, `{ }`, `lambda`, 36 `Proc.new`, `proc`). 37 38 At the other extreme, Javascript offers but one way to define a 39 function: the `function` keyword. It's certainly simple, but especially 40 in callback-oriented code, you wind up writing `function` one hell of a 41 lot. CoffeeScript gives us the `->` operator, combining the brevity of 42 Ruby with the simplicity of Javascript: 43 44 ```coffeescript 45 thrice: (f) -> 46 f() 47 f() 48 f() 49 50 thrice -> puts "OHAI" 51 ``` 52 53 Which translates to: 54 55 ```javascript 56 (function(){ 57 var thrice; 58 59 thrice = function(f) { 60 f(); 61 f(); 62 return f(); 63 }; 64 65 thrice(function() { return puts("OHAI"); }); 66 })(); 67 ``` 68 69 I'll tell you what that is: MONEY. Money in the BANK. 70 71 ## It's Node 72 73 Though not dependent upon it, CoffeeScript is built to run on top of 74 [Node.js](http://nodejs.org/). This means you can take advantage of all 75 the incredible work people are doing with Node, including the 76 [Express](http://expressjs.com/) web framework, the [Redis Node 77 Client](https://github.com/fictorial/redis-node-client), and 78 [Connect](https://github.com/senchalabs/connect), a middleware framework 79 along the lines of [Rack](http://rack.rubyforge.org/). What's more, its 80 integration with Node allows you to run CoffeeScript programs from the 81 command line just like you would Ruby code. 82 83 CoffeeScript is an exciting technology, as both a standalone language 84 and as a piece of a larger Node.js toolkit. Take a look at 85 [Defer](http://gfxmonk.net/2010/07/04/defer-taming-asynchronous-javascript-with-coffeescript.html) 86 to see what the language might soon be capable of, and if you're 87 participating in this year's [Node.js 88 Knockout](http://nodeknockout.com/), watch out for the 89 [Rocketpants](http://nodeknockout.com/teams/2eb41a4c31f50c044a280000).