Slide 1

Slide 1 text

Agile Development With RJS For Ninja Rockstars

Slide 2

Slide 2 text

Much Ado About CoffeeScript Mattt Thompson (@mattt) Lone Star Ruby Conf V

Slide 3

Slide 3 text

3.1

Slide 4

Slide 4 text

Sass CoffeeScript

Slide 5

Slide 5 text

Sass → CSS $blue: #3bbfce $margin: 16px .content-navigation border-color: $blue color: darken($blue, 9%) .border padding: $margin / 2 margin: $margin / 2 border-color: $blue .content-navigation { border-color: #3bbfce; color: #2ca2af; } .border { padding: 8px; margin: 8px; border-color: #3bbfce; }

Slide 6

Slide 6 text

CoffeeScript → JavaScript Account = (customer, cart) -> @customer = customer @cart = cart $('.shopping_cart').bind ↩ 'click', (event) => @customer.purchase @cart var Account; var __bind = function(fn, me) { return function(){ return fn.apply(me, arguments); }; }; Account = function(customer, cart) { this.customer = customer; this.cart = cart; return $ ('.shopping_cart').bind('click', __bind(function(event) { return this.customer.purchase(this.cart); }, this)); };

Slide 7

Slide 7 text

• Installation • Integration With Rails • Syntax • Features Introducing CoffeeScript What We’ll Cover

Slide 8

Slide 8 text

Installation

Slide 9

Slide 9 text

CoffeeScript

Slide 10

Slide 10 text

“It’s just Javascript”

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

• Less Syntactic Cruft

Slide 13

Slide 13 text

• Less Syntactic Cruft • JavaScript Design Patterns are Features of CoffeeScript

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

$ homebrew install node

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

$ curl http://npmjs.org/install.sh | sh

Slide 19

Slide 19 text

$ npm install -g coffee-script

Slide 20

Slide 20 text

$ coffee

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Integration With Rails

Slide 25

Slide 25 text

gem 'rails', '3.1.0rc5'

Slide 26

Slide 26 text

app/ assets/ javascripts/ store.js.coffee

Slide 27

Slide 27 text

Syntax

Slide 28

Slide 28 text

jQuery(function() { $("#title").show(); });

Slide 29

Slide 29 text

jQuery function() $("#title").show()

Slide 30

Slide 30 text

jQuery -> $("#title").show()

Slide 31

Slide 31 text

Store.prototype.add = function(item) { this.items.push(item); }

Slide 32

Slide 32 text

Store.prototype.add = function(item) this.items.push(item)

Slide 33

Slide 33 text

Store.prototype.add = function(item) @items.push(item)

Slide 34

Slide 34 text

add: function(item) @items.push(item)

Slide 35

Slide 35 text

add: -> (item) @items.push(item)

Slide 36

Slide 36 text

add: (item) -> @items.push(item)

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

• Redundant Punctuation Omitted

Slide 39

Slide 39 text

• Redundant Punctuation Omitted • function becomes ->

Slide 40

Slide 40 text

• Redundant Punctuation Omitted • function becomes -> • this. becomes @

Slide 41

Slide 41 text

• Redundant Punctuation Omitted • function becomes -> • this. becomes @ • {} becomes ⇥

Slide 42

Slide 42 text

Features

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Class Pattern 1

Slide 46

Slide 46 text

class Animal constructor: (name) -> @name = name

Slide 47

Slide 47 text

var Animal; Animal = (function() { function Animal(name) { this.name = name; } return Animal; })();

Slide 48

Slide 48 text

2 Lexical Scoping (No Global Variables)

Slide 49

Slide 49 text

window.Animal = class Animal constructor: (name) -> @name = name

Slide 50

Slide 50 text

Default Arguments 3

Slide 51

Slide 51 text

window.Animal = class Animal constructor: (name = "Unknown") -> @name = name

Slide 52

Slide 52 text

String Interpolation 4

Slide 53

Slide 53 text

class Bear extends Animal constructor: (name) -> @name = "#{name}, the Bear"

Slide 54

Slide 54 text

Conditional Suffixes 5

Slide 55

Slide 55 text

@price = amount if amount > 0.00

Slide 56

Slide 56 text

Operator Aliases 6

Slide 57

Slide 57 text

@lovesTacos = false unless @name is "Mattt"

Slide 58

Slide 58 text

CoffeeScript JavaScript is === isnt !== not ! and && or || true, yes, on true @, this this of in in N/A

Slide 59

Slide 59 text

7 Deconstructing Assignment

Slide 60

Slide 60 text

[dollars, cents] = input.match /(\d+)/g

Slide 61

Slide 61 text

Existential Operator 8

Slide 62

Slide 62 text

?

Slide 63

Slide 63 text

[dollars, cents] = (input.match /(\d+)/g) ? [0, 0]

Slide 64

Slide 64 text

Splats 9

Slide 65

Slide 65 text

gold, silver, bronze, rest = "" awardMedals = (first, second, third, others...) -> gold = first silver = second bronze = third rest = others pieEatingCompetitors = [ "Wynn Netherland", "Steve Klabnik", "Adam Keys", "Richard Schneeman", "Rob Mack", "Tim Tyrrell", "Steve Stedman", "Mando Escamilla", "Keith Gaddis" ] awardMedals pieEatingCompetitors...

Slide 66

Slide 66 text

List Comprehensions 10

Slide 67

Slide 67 text

eat food for food in ['toast', 'cheese', 'wine']

Slide 68

Slide 68 text

eat food for food in ['toast', 'cheese', 'wine'] ↩ when food is "toast"

Slide 69

Slide 69 text

languages = { "Javascript": 3, "CoffeeScript": 9, "Ruby": 9, "Python": 6, "Objective-C": 7, "Potion": 10 } favorites = language for language, awesomeness of ↩ languages when awesomeness >= 7

Slide 70

Slide 70 text

• Installation • Integration With Rails • Syntax • Features Introducing CoffeeScript What We’ve Covered So Far

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

“a little language”

Slide 75

Slide 75 text

Web 2.0: A History

Slide 76

Slide 76 text

1995 Ruby Created by Matz JavaScript Created by Brendan Eich

Slide 77

Slide 77 text

2005 Ruby Rails created by DHH JavaScript AJAX coined by Jesse James Garret

Slide 78

Slide 78 text

2006 Ruby RJS shipped with Rails 1.2 JavaScript jQuery 1.0 is released by John Resig

Slide 79

Slide 79 text

page.insert_html :bottom, 'list', content_tag("li", "Fox") page.visual_effect :highlight, 'list', :duration => 3 page.replace_html 'header', 'RJS: Great Success!'

Slide 80

Slide 80 text

2007 Ruby Era of competing Ruby implementations (MRI, YARV, Rubinius, JRuby) JavaScript Era of competing JavaScript frameworks (Prototype, jQuery, dojo, MooTools, YUI)

Slide 81

Slide 81 text

2008 Ruby Cartoon Foxes JavaScript Unicorns

Slide 82

Slide 82 text

2009 Ruby Ruby 1.9 released JavaScript Node.js created by Ryan Dahl

Slide 83

Slide 83 text

2010 Ruby CoffeeScript JavaScript CoffeeScript

Slide 84

Slide 84 text

♕ KEEP CALM AND CARRY ON

Slide 85

Slide 85 text

⽂ WAKE UP AND SMELL THE COFFEE

Slide 86

Slide 86 text

FORGET THAT AND FREAK OUT ♕

Slide 87

Slide 87 text

⚒ GET EXCITED AND MAKE THINGS

Slide 88

Slide 88 text

Much Ado About CoffeeScript Mattt Thompson (@mattt) Lone Star Ruby Conf V