web development with ruby - from simple to complex

63
twitter: @bphogan email: brianhogan at napcs.com Like this talk? Rate it at http://spkr8.com/t/4773 Web Development With Ruby From simple to complex 1 Sunday, October 10, 2010

Upload: brian-hogan

Post on 22-Nov-2014

5.132 views

Category:

Technology


1 download

DESCRIPTION

Beyond the massive hype of Ruby on Rails, there's an amazing world of frameworks, DSLs, and libraries that make the Ruby language a compelling choice when working on the web. In this talk, you'll get a chance to see how to use Ruby to quickly build a static web site, create complex stylesheets with ease, build a simple web service, crete a simple Websocket server, and test your existing applications. Finally, you'll see a few of the ways Rails really can make developing complex applications easier, from advanced database querying to rendering views in multiple formats.

TRANSCRIPT

Page 1: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Web Development With RubyFrom simple to complex

1Sunday, October 10, 2010

Page 2: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Ruby developers have greatly influenced web

development.

2Sunday, October 10, 2010

Page 3: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

ASP.Net MVC and NuPack

3Sunday, October 10, 2010

Pretty hot stuff. Inspired by Rails and RubyGems, two things we Rubyists have used to build quality stuff for years.

Page 4: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

With Ruby we can•Make HTML and CSS better•Quickly prototype or build simple web sites•Automate complicated builds or deployments•Build simple micro-apps•Implement Web Sockets servers•Improve communication between customers and developers•while also testing our web sites•Build complex apps that interface with legacy systems

4Sunday, October 10, 2010

Page 5: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

All made possible by the highly dynamic

features of the

5Sunday, October 10, 2010

Page 6: Web Development With Ruby - From Simple To Complex

So, what is Ruby?• Highly dynamic

• Very high level

• 100% object oriented

• 100% open-source

• Really easy to learn

6Sunday, October 10, 2010

Highly dynamic, high level, 100% object oriented, 100% open source, and really easy to learn.

Page 7: Web Development With Ruby - From Simple To Complex

twitter: bphoganemail: brianhogan at napcs.com

History

Smalltalk(1983)

C++ (1989)

Ruby(1993)

Java(1995)

VB 6(1996)

C#(2000)

7Sunday, October 10, 2010

Ruby was created by Yukihiro Matsumoto (Matz) in 1993. It’s built on C, and has many implementations, including JRuby, which runs on the JVM, and IronRuby, which runs on the .Net platform.

Page 8: Web Development With Ruby - From Simple To Complex

twitter: bphoganemail: brianhogan at napcs.com

8Sunday, October 10, 2010

“How you feel is more important than what you do. “The entire language is designed for programmer productivity and fun.

Page 9: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Basic Ruby

9Sunday, October 10, 2010

Page 10: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

5 + 510 * 10"Hello" + "World"25 / 5

10Sunday, October 10, 2010

We have numbers, strings, multiplication, addition, subtraction, and division, just like everyone else.

Page 11: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

age = 42first_name = "Homer"start_date = Date.new 1980, 06, 05annual_salary = 100000.00

11Sunday, October 10, 2010

It also helps that the syntax is simple. There are no unnecessary semicolons or curly braces. The interpreter knows when lines end.

Page 12: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Ruby follows the Principle of Least

Surprise.

12Sunday, October 10, 2010

Principle of Least Surprise - This means The language should behave in a way that is not confusing to experienced developers. It doesn’t mean that it works like your current favorite language! But as you get used to Ruby, you’ll find that you ramp up quickly.

Page 13: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

"Brian".length["red", "green", "blue"].length[:first_name => "Brian", :last_name => "Hogan"].lengthUser.find_all_by_last_name("Hogan").length

13Sunday, October 10, 2010

Ruby achieves this through a consistant API. You won’t find yourself guessing too much what methods are available to you.

Page 14: Web Development With Ruby - From Simple To Complex

Arrays

colors = ["Red", "Green", "Blue"]

14Sunday, October 10, 2010

The square brackets denote an array.

Page 15: Web Development With Ruby - From Simple To Complex

Hashes (Dictionaries)

attributes = {:age => 25, :first_name => "Homer", :last_name => "Simpson"}

15Sunday, October 10, 2010

Page 16: Web Development With Ruby - From Simple To Complex

=>

16Sunday, October 10, 2010

This is the hash symbol, or the hash rocket. Whenever you see this, you’re dealing with a hash.

Page 17: Web Development With Ruby - From Simple To Complex

twitter: bphoganemail: brianhogan at napcs.com

:foo

17Sunday, October 10, 2010

When you see these, you’re looking at Symbols. They represent names and some strings. They conserve memory, as repeating a symbol in your code uses the same memory reference, whereas repeating a string creates a new object on each use.

Page 18: Web Development With Ruby - From Simple To Complex

Simple control logic

if on_probation(start_date) puts "Yes"else puts "no"end

18Sunday, October 10, 2010

Page 19: Web Development With Ruby - From Simple To Complex

Methods (functions) are simple too.

# if start date + 6 months is > todaydef on_probation?(start_date) (start_date >> 6) > Date.todayend

19Sunday, October 10, 2010

The two arrows (>>) is actually a method on the Date object that adds months. So here, we’re adding six months to the start date and comparing it to today

Notice here that the input parameter is assumed to be a date. There’s no type checking here.

Page 20: Web Development With Ruby - From Simple To Complex

Let Ruby write code for you!

class Person @started_on = Date.today @name = "" def started_on=(date) @started_on = date end def started_on @started_on end

def name=(name) @name = name end def name @name endend

class Person attr_accessor :name attr_accessor :started_onend

20Sunday, October 10, 2010

Making getters and setters is so common that Ruby can do it for you.

Page 21: Web Development With Ruby - From Simple To Complex

def test_user_hired_today_should_be_on_probation person = Person.new person.hired_on = Date.today assert person.on_probation?end

test_user_hired_last_year_should_not_be_on_probation person = Person.new person.hired_on = 1.year.ago assert !person.on_probation?end

21Sunday, October 10, 2010

Here we have two tests, one using a person hired today, and another using a person last year.

Page 22: Web Development With Ruby - From Simple To Complex

Implement the method

class Person attr_accessor :name, :start_date def on_probation? (start_date >> 6) > Date.today endend

22Sunday, October 10, 2010

Watch as the tests pass.

Page 23: Web Development With Ruby - From Simple To Complex

twitter: bphoganemail: brianhogan at napcs.com

haml and sass

23Sunday, October 10, 2010

Page 24: Web Development With Ruby - From Simple To Complex

HAML

!!!#wrapper.container_12 #header.grid_12 %h1 The awesome site %ul#navbar.grid_12 %li %a{:href => "index.html"} Home %li %a{:href => "products"} Products %li %a{:href => "services"} Services #middle.grid_12 %h2 Welcome #footer.grid_12 %p Copyright 2010 AwesomeCo

24Sunday, October 10, 2010

Page 25: Web Development With Ruby - From Simple To Complex

HTML <div class='container_12' id='wrapper'> <div class='grid_12' id='header'> <h1>The awesome site</h1> </div> <ul class='grid_12' id='navbar'> <li> <a href='index.html'>Home</a> </li> <li> <a href='products'>Products</a> </li> <li> <a href='services'>Services</a> </li> </ul> <div class='grid_12' id='middle'> <h2>Welcome</h2> </div> <div class='grid_12' id='footer'> <p>Copyright 2010 AwesomeCo</p> </div> </div>

25Sunday, October 10, 2010

Page 26: Web Development With Ruby - From Simple To Complex

SASS

$the_border: 1px$base_color: #111

#header color: $base_color * 3 border-left: $the_border border-right: $the_border * 2 color: red a font-weight: bold text-decoration: none

26Sunday, October 10, 2010

Page 27: Web Development With Ruby - From Simple To Complex

CSS

#header { color: #333333; border-left: 1px; border-right: 2px; color: red; }

#header a { font-weight: bold; text-decoration: none; }

27Sunday, October 10, 2010

Page 28: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

$the_border: 1px$base_color: #111

#header color: $base_color * 3 border-left: $the_border border-right: $the_border * 2 color: red a font-weight: bold text-decoration: none

#header { color: #333333; border-left: 1px; border-right: 2px; color: red; } #header a { font-weight: bold; text-decoration: none; }

28Sunday, October 10, 2010

Page 29: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Demos

29Sunday, October 10, 2010

Page 30: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

StaticMatichttp://staticmatic.rubyforge.org/

30Sunday, October 10, 2010

Page 31: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

src layouts application.haml pages index.html stylesheets application.sass

site index.html images javascripts stylesheets application.css

31Sunday, October 10, 2010

StaticMatic is a tiny framework for building static websites quickly using HAML and SASS.

Page 32: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Rake

32Sunday, October 10, 2010

Page 33: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Automation LanguageDeploy with a single command!

33Sunday, October 10, 2010

Page 34: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Rakefiles have tasks

desc "Copies the site to our remote server"task :deploy do puts "*** Deploying the site via SSH to #{ssh_user}" system("rsync -avz --delete #{upload_files}/ #{ssh_user}:#{remote_root}") FileUtils.rm_rf upload_files rescue nilend

rake deploy

34Sunday, October 10, 2010

Page 35: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

StaticMatic and Rake demo

35Sunday, October 10, 2010

Let’s put a site online

Page 36: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Sinatrahttp://www.sinatrarb.com/intro

36Sunday, October 10, 2010

Sinatra is a great way to build simple web apps with Ruby.

Page 37: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Great for micro apps

37Sunday, October 10, 2010

Page 38: Web Development With Ruby - From Simple To Complex

Hello Sinatra!

require 'rubygems'require 'sinatra'

get "/" do "Hello Sinatra!"end

38Sunday, October 10, 2010

Sinatra is a simple web framework that basically maps incoming requests to backend code that produces responses.

Page 39: Web Development With Ruby - From Simple To Complex

39Sunday, October 10, 2010

That little bit of code gets us a working web application that handles requests.

Page 40: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Sinatra demos

40Sunday, October 10, 2010

Page 41: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Composable apps?Sure!

41Sunday, October 10, 2010

Page 42: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

config.ru

require 'rubygems'require 'sinatra' # include our Application coderequire File.join(File.dirname(__FILE__), 'main')require File.join(File.dirname(__FILE__), 'blog') # disable sinatra's auto-application startingdisable :run map "/" do run Mainend

map "/blog" do run Blogend

Sinatra Apps

Mountingto URLs

42Sunday, October 10, 2010

Page 43: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Testing Web Apps

43Sunday, October 10, 2010

Page 44: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

cucumberWeb application testing

for people first and computers second

44Sunday, October 10, 2010

Page 45: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Plain Text Scenarios!Feature: an advanced google search with CucumberAs an interested developer who wants to automate tasks with CucumberI want to search GoogleSo that I can find more information on how it works.

Scenario: Advanced searchGiven I go to "http://www.google.com"And I click "Advanced search"And I fill in "as_q" with "cucumber"And I fill in the "any of these unwanted words" field with "pickles"And I select "50 results" from the "Number of results" dropdownAnd I click the Date, usage rights, numeric range, and more sectionAnd I turn on Safe SearchWhen I press "Advanced Search"Then I should see "Cucumber - Making BDD fun"When I click "Cucumber - Making BDD fun" And I click "Wiki" And I click "Gherkin" And I click "Feature Introduction"Then I should see "Feature Introduction"

45Sunday, October 10, 2010

Page 46: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Express requirementsplainly...

46Sunday, October 10, 2010

Page 47: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

then run real browsers!

47Sunday, October 10, 2010

Page 48: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Demo?

48Sunday, October 10, 2010

Page 49: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

RadiantContent Management System

49Sunday, October 10, 2010

Based on Rails, completely modular, easy to install

Page 50: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Web Sockets

50Sunday, October 10, 2010

Page 51: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

NodeJS? NoWai!

51Sunday, October 10, 2010

Page 52: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

EventMachine

52Sunday, October 10, 2010

Page 53: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Demo?

53Sunday, October 10, 2010

Fi

Page 54: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Finally, Rails

54Sunday, October 10, 2010

Page 55: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Opinionated, but flexible.

55Sunday, October 10, 2010

Page 56: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Demo

56Sunday, October 10, 2010

Page 57: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

“Use The Right Tool”

57Sunday, October 10, 2010

We hear this a lot.

Page 58: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Most people who advocate that mean

“The right tool is the one I use”

58Sunday, October 10, 2010

Page 59: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

I am a “Web” developer first and a Ruby

developer second.

59Sunday, October 10, 2010

Page 60: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Ruby is the fastest, best, most productive, and most stable, and lucrative way to build

web stuff...

60Sunday, October 10, 2010

Page 61: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

...for me, for now.

61Sunday, October 10, 2010

But I still use WordPress for my blogs, I still use PHP for simple web stuff, and I still use Visual Basic to maintain a commercial app I sold years ago. I use ASP

Page 62: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

Code examples

http://github.com/napcs/cucumber_watir

http://dl.dropbox.com/u/50783/tccc9_ruby_webdev.zip

62Sunday, October 10, 2010

Page 63: Web Development With Ruby - From Simple To Complex

twitter: @bphoganemail: brianhogan at napcs.comLike this talk? Rate it at http://spkr8.com/t/4773

You have new tools.Go use them.

63Sunday, October 10, 2010