Ruby on Rails 3.1: Let's bring the fun back into web programing

Preview:

DESCRIPTION

The first talk from the "Empower on Rails" Ruby on Rails seminars.

Citation preview

Ruby on Rails 3.1Let’s bring the fun back to web programming!

presents

Bozhidar BatsovTechnical Lead

The problem

Many programmers are not particularly happy

It's the question that drives us, Neo. It's the question that brought you here. You know the question, just as I did.

What is the question?

How can I be be one happy

(web) programmer?

VS. VS.

VS. VS.

This PHP code is so clean and elegantly solves the problem at hand.

Nobody, Never

0 10 20 30 40 50 60 70 80 90 100OOP

FP

Performance

Productivity

Fun

PHP

There should be one – and preferably only one – obvious way to do it.

The Zen of Python

0 10 20 30 40 50 60 70 80 90 100OOP

FP

Performance

Productivity

Fun

Python

The Web framework for perfectionists with

deadlines.

2007

A Programmer’s Best Friend

The goal of Ruby is to make programmers happy. I started out to make a programming language that would make me happy, and as a side effect it’s made many, many programmers happy. Especially Web developers.

Yukihiro “Matz” Matsumoto,creator of Ruby

0 10 20 30 40 50 60 70 80 90 100

OOP

FP

Performance

Productivity

Fun

Ruby

0 10 20 30 40 50 60 70 80 90 100

OOP

FP

Performance

Productivity

Fun

PHP Python Ruby

0369

12

15

18

21

24

27

30

JQueryDjango

Ruby on Rails

Startup Technologies 2011

http://www.ruby-toolbox.com

“Rails is the most well thought-out web development framework I’ve ever used. And

that’s in a decade of doing web applications for a living. I’ve built my own frameworks, helped

develop the Servlet API, and have created more than a few web servers from scratch. Nobody

has done it like this before.”

James Duncan Davidson, Creator of Tomcat and Ant

“Ruby on Rails is a breakthrough in lowering the barriers of entry to

programming. Powerful web applications that formerly might have taken weeks or months to develop can

be produced in a matter of days.”

Tim O'Reilly, Founder of O'Reilly Media

“It is impossible not to notice Ruby on Rails. It has had a huge effect both in and outside the Ruby community...

Rails has become a standard to which even well-established tools are

comparing themselves to.”

-Martin Fowler, Author of Refactoring, PoEAA, XP Explained

“Rails is the killer app for Ruby.”

Yukihiro Matsumoto, Creator of Ruby

Convention over

Configuration

DRY(Don’t repeat

yourself)

Many view (template) options

#profile .left.column #date= print_date #address= current_user.address .right.column #email= current_user.email #bio= current_user.bio

<div id="profile"> <div class="left column"> <div id="date"><%= print_date %></div> <div id="address"><%= current_user.address %></div> </div> <div class="right column"> <div id="email"><%= current_user.email %></div> <div id="bio"><%= current_user.bio %></div> </div></div>

HamlHTML + Erb

doctype htmlhtml head title Slim Core Example meta name="keywords" content="template language"

body h1 Markup examples

div id="content" class="example1" p Nest by indentation

== yield

- unless items.empty? table - for item in items do tr td = item.name td = item.price - else p | No items found. Please add some inventory. Thank you!

div id="footer" | Copyright © 2010 Andrew Stone

= render 'tracking_code'

javascript: $(content).do_something();

Testing in Rails is not optional!

All the code is guilty until

proven innocent!

DSL FTW

class Page < ActiveRecord::Base has_many :page_images, :dependent => :destroy

validates :title, :presence => true, :uniqueness => true validates :content, :presence => true validates :permalink, :presence => true, :uniqueness => true

accepts_nested_attributes_for :page_images, :allow_destroy => true

def to_param permalink endend

ActiveRecord

XML Freedevelopment: adapter: postgresql database: mycoolproject host: localhost username: mycoolproject password: mycoolproject encoding: utf8

test: adapter: sqlite3 database: db/test.sqlite3 pool: 5 timeout: 5000

rails g (generate)

rails c (console)

rails db (dbconsole)

rake

bundle

UNIX Certified

Agile

Innovation

Lots of friends

Fork me on GitHub!

Fantastic documentation

RailsGuides (http://guide.rubyonrails.org)

RailsCasts (http://railscasts.org)

PragProg

Ruby on Rails 3 Tutorial (http://ruby.railstutorial.org/)

The Dark Art of Deployment

In the clouds...

?

$ heroku create --stack cedar$ git push heroku master$ heroku open$ heroku scale web=100 worker=50

Happy programmers

Rails 3.1

CoffeeScript# Assignment:number = 42opposite = true

# Conditions:number = -42 if opposite

# Functions:square = (x) -> x * x

# Arrays:list = [1, 2, 3, 4, 5]

# Objects:math = root: Math.sqrt square: square cube: (x) -> x * square x

# Splats:race = (winner, runners...) -> print winner, runners

# Existence:alert "I knew it!" if elvis?

# Array comprehensions:cubes = (math.cube num for num in list)

var cubes, list, math, num, number, opposite, race, square;var __slice = Array.prototype.slice;number = 42;opposite = true;if (opposite) number = -42;square = function(x) { return x * x;};list = [1, 2, 3, 4, 5];math = { root: Math.sqrt, square: square, cube: function(x) { return x * square(x); }};race = function() { var runners, winner; winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return print(winner, runners);};if (typeof elvis !== "undefined" && elvis !== null) alert("I knew it!");cubes = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = list.length; _i < _len; _i++) { num = list[_i]; _results.push(math.cube(num)); } return _results;})();

JavaScript

Asset Pipeline

SASS$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: #2b9eab;}

.border { padding: 8px; margin: 8px; border-color: #3bbfce;}

table.hl { margin: 2em 0; td.ln { text-align: right; }}

li { font: { family: serif; weight: bold; size: 1.2em; }}

table.hl { margin: 2em 0;}table.hl td.ln { text-align: right;}

li { font-family: serif; font-weight: bold; font-size: 1.2em;}

@mixin table-base { th { text-align: center; font-weight: bold; } td, th {padding: 2px}}

@mixin left($dist) { float: left; margin-left: $dist;}

#data { @include left(10px); @include table-base;}

#data { float: left; margin-left: 10px;}#data th { text-align: center; font-weight: bold;}#data td, #data th { padding: 2px;}

.error { border: 1px #f00; background: #fdd;}.error.intrusion { font-size: 1.3em; font-weight: bold;}

.badError { @extend .error; border-width: 3px;}

.error, .badError { border: 1px #f00; background: #fdd;}

.error.intrusion,

.badError.intrusion { font-size: 1.3em; font-weight: bold;}

.badError { border-width: 3px;}

Always in motion the future is...

Rails 4.0

targeting Ruby 1.9.2

expected in an year

it will be the end of the world as we know it :-)

About Bozhidar

bozhidar@empowerunited.com

http://batsov.com

@bbatsov

http://github.com/bbatsov

Q & A(any questions?!)

Thanks!

Don’t leave just yet ;-)

you@empowerunited.comfacebook.com/empoweronrails