rails 3 beginner to builder 2011 week 1
DESCRIPTION
This is the first of 8 presentations given at University of Texas during my Beginner to Builder Rails 3 Class. For more info and the whole series including video presentations at my blog: http://schneems.tumblr.com/tagged/Rails-3-beginner-to-builder-2011TRANSCRIPT
June, 2011
Beginner To BuilderWeek 1Richard Schneeman@schneems
Friday, June 10, 2011
@Schneems
Background
• Georgia Tech
• 5 Years of Rails
• Rails dev for Gowalla
Friday, June 10, 2011
@Schneems
About the Class• Why?
• Structure
• Class & Course Work
• Ruby through Rails
• 8 Weeks
• 1 hour
Friday, June 10, 2011
@Schneems
Rails - Week 1• Ruby
• Versus Rails
• Data Types
• Rails Architecture
• MVC (Model View Controller)
• ORM (Object Relational Mapping)
• RESTful (REpresentational STate)
Friday, June 10, 2011
@Schneems
Rails - Week 1•Workspace
• Version Control - Keep your code safe
• RubyGems - Use other’s code
• Bundler - Manage Dependencies
• RVM - Keep your system clean
• Tests - make sure it works
Friday, June 10, 2011
@Schneems
Ruby Versus Rails• Ruby - Is a programming Language
• Like C# or Python
• Can be used to program just about anything
• Rails - Is a Framework
• Provides common web functionality
• Focus on your app, not on low level details
Friday, June 10, 2011
@Schneems
Rails is a Web Framework• Develop, deploy, and maintain dynamic web apps
• Written using Ruby
• Extremely flexible, expressive, and quick development time
Friday, June 10, 2011
@Schneems
Technologies• Html - creates a view
• Javascript - makes it interactive
• css - makes it pretty
• Ruby - Makes it a web app
Friday, June 10, 2011
@SchneemsFriday, June 10, 2011
@Schneems
Ruby Resources• why’s (poignant) guide to ruby
• Free, quirky
• Programming Ruby (Pickaxe)
• Not free, encyclopedic
Friday, June 10, 2011
@Schneems
Ruby Resources• Metaprogramming Ruby
• Skips basics
• Unleash the power of Ruby
Friday, June 10, 2011
@Schneems
Interactive Ruby Console(IRB) or http://TryRuby.org/
Friday, June 10, 2011
@Schneems
Ruby Strings• Characters (letters, digits, punctuation)
surrounded by quotes
food = "chunky bacon"
puts "I'm hungry for, #{food}!"
>> "I'm hungry for, chunky bacon!"
"I'm hungry for, chunky bacon!".class
>> String
Friday, June 10, 2011
@Schneems
Ruby (numbers)
123.class
>> Fixnum
(123.0).class
>> Float
Friday, June 10, 2011
@Schneems
Ruby Symbols•Symbols are lightweight strings
• start with a colon
• immutable
:a, :b or :why_the_lucky_stiff
:why_the_lucky_stiff.class
>> Symbol
Friday, June 10, 2011
@Schneems
Ruby Hash•A hash is a dictionary surrounded by curly
braces.
•Dictionaries match words with their definitions.
my_var = {:sup => "dog", :foo => "bar"}
my_var[:foo]
>> "bar"
{:sup => "dog", :foo => "bar"}.class
>> Hash
Friday, June 10, 2011
@Schneems
Ruby Array•An array is a list surrounded by square
brackets and separated by commas.
array = [ 1, 2, 3, 4 ]
array.first
>> 1
[ 1, 2, 3, 4 ].class
>> Array
Friday, June 10, 2011
@Schneems
Ruby Array•Zero Indexed
array = [ 1, 2, 3, 4 ]
array[0]
>> 1
array = [ 1, 2, 3, 4 ]
array[1]
>> 2
Friday, June 10, 2011
@Schneems
Ruby Blocks•Code surrounded by curly braces
2.times { puts "hello"}
>> "hello"
>> "hello"
•Do and end can be used instead2.times do
puts "hello"
end
>> "hello"
>> "hello"
Friday, June 10, 2011
@Schneems
Ruby Blocks•Can take arguments
• variables surrounded by pipe (|)
2.times do |i|
puts "hello #{i}"
end
>> "hello 0"
>> "hello 1"
Friday, June 10, 2011
@Schneems
Rails why or why-not?• Speed
• developer vs computer
• Opinionated framework
• Quick moving ecosystem
Friday, June 10, 2011
@Schneems
Rails Architecture• Terminology
• DRY
• Convention over Configuration
• Rails Architecture
• MVC (Model View Controller)
• ORM (Object Relational Mapping)
• RESTful (REpresentational State Transfer)
Friday, June 10, 2011
@Schneems
DRYDon’t Repeat Yourself
Reuse, don’t re-invent the...
Friday, June 10, 2011
@Schneems
Convention over Configuration
Decrease the number of decisions needed, gaining simplicity but without losing flexibility.
Friday, June 10, 2011
@Schneems
• Isolates “Domain Logic”
• Can I See it?
• View
• Is it Business Logic?
• Controller
• Is it a Reusable Class Logic?
• Model
Model-View-Controller
Friday, June 10, 2011
@Schneems
Model-View-Controller• Generated By Rails
• Grouped by Folders
• Connected “AutoMagically”
• Models
• Views
• Controllers
• Multiple Views Per Controller
Friday, June 10, 2011
@Schneems
Database Backed Models• Store and access massive amounts of
data
• Table
• columns (name, type, modifier)
• rows
Table: Users
Friday, June 10, 2011
• Structured Query Language
• A way to talk to databases
@Schneems
SELECT *
FROM Book
WHERE price > 100.00
ORDER BY title;
SQL
Friday, June 10, 2011
@Schneems
SQL operations• insert
• query
• update and delete
• schema creation and modification
Friday, June 10, 2011
@Schneems
Object Relational Mapping• Maps database backend to ruby objects
• ActiveRecord (Rail’s Default ORM)>> userVariable = User.where(:name => "Bob")
>> userVariable.name=> Bob
Generates:SELECT \"users\".* FROM \"users\"WHERE (name = 'bob')
Friday, June 10, 2011
@Schneems
• >> userVariable = User .where(:name => "Bob")
models/user.rb
the User class inherits from ActiveRecord::Base
Object Relational Mapping
class User < ActiveRecord::Base
end
Friday, June 10, 2011
@Schneems
Object Relational Mapping
• >> userVariable = User. where(:name => "Bob")
where is the method that looks in the databaseAutoMagically in the User Table (if you made one)
Friday, June 10, 2011
@Schneems
RESTful
• The state of the message matters
• Different state = different message
REpresentational State Transfer
“You Again?” “You Again?”
Friday, June 10, 2011
@Schneems
RESTful• Servers don’t care about smiles
• They do care about how you access them
• (HTTP Methods)
• GET
• PUT
• POST
• DELETE
REpresentational State Transfer
Friday, June 10, 2011
@Schneems
RESTful• Rails Maps Actions to HTTP Methods
• GET - index, show, new
• PUT - update
• POST - create
• DELETE - destroy
REpresentational State Transfer
Friday, June 10, 2011
@Schneems
Work Environment• Version Control - Keep your code safe
• RubyGems - Use other’s code
• Bundler - Manage Dependencies
• RVM - Keep your system clean
• Tests - make sure it works
Friday, June 10, 2011
@Schneems
Version Control• my_last_update_1.rb
• my_realy_last_update_2.rb
• really_the_good_code_last_final_new.rb
Friday, June 10, 2011
@Schneems
Version Control• my_last_update_1.rb
• my_realy_last_update_2.rb
• really_the_good_code_last_final_new.rb
Friday, June 10, 2011
@Schneems
Version Control• my_last_update_1.rb
• my_realy_last_update_2.rb
• really_the_good_code_last_final_new.rb
Friday, June 10, 2011
@Schneems
Version Control• Make note of whats different
• See changes over time
• revert back to known state
• work with a team
Friday, June 10, 2011
@Schneems
Version Control• Git (recommended)
• SVN
• Mecurial
• Perforce
• Many More
Friday, June 10, 2011
@Schneems
RubyGems• Gems
• External code “packages”
• Rubygems Manages these “packages”
gem install keytar
irb
>> require “rubygems”
=> true
>> require “keytar”
=> true
Friday, June 10, 2011
@SchneemsFriday, June 10, 2011
• Install
• Gemfiles
• specify dependencies
@Schneems
gem install bundler
Bundler
source :rubygems
gem 'rails', '3.0.4'
gem 'unicorn', '3.5.0'
gem 'keytar'
Friday, June 10, 2011
• Install
• installs all gems listed in gemfile
• very useful managing across systems
@Schneems
bundle install
Bundler
Friday, June 10, 2011
• Ruby Version Manager
• Clean Sandbox for each project
@Schneems
RVM
rvm use ruby-1.8.7-p302
rvm use ruby-1.9.2-p180
Friday, June 10, 2011
• Use fresh gemset for each project
• Switch projects...switch gemsets
@Schneems
RVM
rvm gemset use gowalla
rvm gemset use keytar
Friday, June 10, 2011
@Schneems
Testing• Does your code 6 months ago work?
• What did it do again?
• Manual Versus Programatic
• Save Time in the long road by progamatic Testing
Friday, June 10, 2011
@Schneems
Testing• Test framework built into Rails
• Swap in other frame works
• Use Continuous Integration (CI)
• All tests green
• When your web app breaks, write a test for it (never again)
Friday, June 10, 2011
@Schneems
IDE• Mac: Textmate
• Windows: Notepad ++
• Eclipse & Aptana RadRails
Friday, June 10, 2011
@Schneems
Recap• Rails
• Framework
• Convention over Configuration
• Ruby
• Expressive Scripting language
Friday, June 10, 2011
@Schneems
Questions?
Friday, June 10, 2011