cheat sheets.

$ command line ruby cheat sheets
### Usage

most commonly used parameters from coffeescript.org
-c, --compile
-i, --interactive
-o, --output [DIR]
-p, --print
-e, --eval
--nodejs

### Functions

#Last expression value is return value
add(a, b) -> a + b
add(1, 2) == 3

# internal variables override outer ones

message = 'Outer'
getMessage = -> message
overrideMessage = -> message = 'Inner'
overrideMessage() == 'Inner'

# supports splats
returnAllArgs = (allargs...) -> allargs
returnAllArgs('first', 'second', 'third') == ['first', 'second', 'third']

returnAllButFirst = (firstArg, rest...) -> rest
returnAllBut­First('first', 'second', 'third') == ['second', 'third']

#destructuring assignment
weatherReport = (location) -> [location, 22, 'Mostly sunny']
[city, temper­ature, forecast] = weatherReport 'London'
city == 'London'
temper­ature == 22

### Higher Order

2 in [1..3] == true

#Javascript-style filter
even = (a) -> a % 2 == 0
[1..6].filter even == [2, 4, 6]

### Coffee­Script-style filter
odds = (x for x in [1..6] when not even x)

### Coffee­Script-style map
twice = (a) -> a * 2
doubles = (twice x for x in [1..6])
  
###Objects
meglom­aniac = {}

beforeEach ->
meglom­aniac =
mastermind: 'The Monarch'
henchwoman: 'Dr Girlfriend'
theBomb: true

# ? existence operator
meglom­aniac.theBomb? == true 
meglom­aniac.theDetonator? == false

# properties can be added and deleted
meglom­aniac.­master­mind2 = 'Agent Smith'
delete meglom­ani­ac.­mas­termind

# prototype to add to all projects
Circle = (radius) -> @radius = radius

'­@­' = 'this context' In Coffee­script
Inheritance
# running example from Muppets

class Muppet
constr­uctor: (@age, @hobby) ->
answer­Nanny: -> "Everything's cool!"

class Swedis­hChef extends Muppet
constructor: (age, hobby, @mood) ->
super(age, hobby)
cook: -> 'Mmmm soup!'

@swedis­hChef = new Swedis­hChef 3, 'cooking', 'chillin'

@swedis­hCh­ef.cook() == 'Mmmm soup!'

#base object
@swedis­hCh­ef.­ans­wer­Nanny() == "Everything's cool!"

#instances to override class methods
gonzo = new Muppet 3, 'daredevil performer'
gonzo.­answer­Nanny = -> 'Hehehe!'
Hello World
echo "consol­e.log 'Hello World'" > hello.­coffee
coffee hello.­coffee
  
Arrays
[1..5] == [1, 2, 3, 4, 5]
[1...5] == [1, 2, 3, 4] # extra dot
[3..1] == [3, 2, 1]

fourNumberArray = [1, 2, 3, 4]
fourNumberAr­ray­.push(5, 6)
fourNumberArray == [1, 2, 3, 4, 5, 6]

[1..10][3..5] == [4, 5, 6] # range slicing

"my string"[0..1] == "my" # string slicing

copyOf­Array = array.­slice()
Array Reduction
# Javascript style
total = (i, a) -> i + a
reduction = [1..3].reduce total == 6

# Coffee­script-style
total = 0
sum = (a) -> total = total + a
sum x for x in [1..3]
total == 6
MDN Docume­ntation:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Reduce
Credits
This cheat sheet is (mostly) based on work of sleepyfox, which can be found here: https://github.com/sleepyfox/coffeescript-koans

Coffee­Script @github: http://jashke­nas.github.com/coffee-script/
Misc
Official Site: http://coffee­scr­ipt.org/

Textmate Bundle: https://github.com/jashkenas/coffee-script-tmbundle

Book: http://pragpr­og.com/book/tbcoffee/coffee­script