Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save spacekat/1228271 to your computer and use it in GitHub Desktop.
Save spacekat/1228271 to your computer and use it in GitHub Desktop.
CoffeeScript basics from Rails Girls, Helsinki, 2011
# =================================================================
# = Examples 1. Simple data storage (Variables, Strings, Numbers) =
# =================================================================
# String assignment
theSingle = "Video Killed the Radio Star"
theBand = "The Buggles"
# Numbers
mtvMusicVideoDebut = 1981
billboardPosition = 40
# String Interpolation
alert "#{ theSingle } by #{ theBand }"
# ========================
# = Example 2. Functions =
# ========================
# Anonymous function
->
alert "I love rock-n-roll"
# Named Function
singIt = () ->
alert "I love rock-n-roll!"
# Calling a function
singIt()
# Passing a variable
singSomethingElse = (lyrics) ->
alert lyrics
singSomethingElse "Don't go wasting your emotion"
singSomethingElse "Lay all your love on me"
# Passing many variables
manyLines = (chorusLineOne, chorusLineTwo) ->
alert chorusLineOne
alert chorusLineTwo
singIt()
chorusLineOne = "Don't go wasting your emotion"
chorusLineTwo = "Lay all your love on me"
manyLines(chorusLineOne,chorusLineTwo)
# =============================
# = Objects and Comprehensions =
# =============================
# Key : Value pairs
quiteMoody =
monday: "feel blue"
tuesday: "feel gray"
wednesday: "feel gray again"
thursday: "do not care about you"
friday: "am in love"
# Accessing individual values
alert quiteMoody.tuesday
# Go over the object with for i, j of
feelings = for day, mood of quiteMoody
"On #{day} I #{mood}"
# Returning the value of the comprension
alert feelings.join(", ")
# =============================
# = Arrays and Comprehensions =
# =============================
# Array brackets
tracks = [
"One More Chance"
"What Have I Done to Deserve This?"
"Shopping"
"Rent"
"Hit Music"
"It Couldn't Happen Here"
"It's a Sin"
"I Want to Wake Up"
"Heart"
"King's Cross"
]
# Array comprehension returns another array
shortTrackNames = (trackName for trackName in tracks when name.length 10 )
alert shortTrackNames.join(", ")
# ==============
# = Conditions =
# ==============
# if, is, else...
firstRequest = (album) ->
if album is "London Calling"
"The Guns of Brixton"
else if album is "Talk Talk Talk"
"Pretty in Pink"
else if album is "The Circus"
"Sometimes"
else
"put your iPod on shuffle"
alert firstRequest("The Circus")
# ===================
# = More Conditions =
# ===================
# yes, no, true, false
youNeedAFriend = yes
lookToAStranger = no
if youNeedAFriend is true and lookToAStranger is false
alert "I will be there"
# ===================
# = jQuery examples =
# ===================
# Selectors and chaining
$("h1").css("color", "red")
$("li").slideDown("slow")
$("img.growup").click ->
animate({
height: "441px"
width: "460px"
}, 5000)
# ====================
# = Pattern matching =
# ====================
# It looks like an array, but it's not!
# The left side is definine a pattern of variable to match with the values on the right side.
[firstName, middle, last] = ["Steven", "Patrick", "Morrissey"]
alert firstName + " " + middle + " " + last
# Referencing the same variable on either side
bassistShoes = "Chucks"
drummerShoes = "Vans"
[bassistShoes, drummerShoes] = [drummerShoes, bassistShoes]
alert "The drummer is wearing #{ drummerShoes }."
alert "The bassist is wearing #{ bassistShoes }."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment