Dano Manion

Ruby Notes

Character Escape Sequences

Escape Sequences

  • puts is a print with a \n on the end.
  • .chomp IE gets.chomp or gets.strip strps off and removes spaces

Awesome Truth Tables

Boolean logic expressions

  • ! Not is an invertor, if you give a 1 you get a 0 if you get a 1 then you get a 0 truth tables


  • The way we learn numbers and order
    • ordinal == ordered, 1st;
  • The way Arrays store numbers
    • cardinal == cards at random, 0.
  • What’s a data structure? If you think about it, a “data structure” is just a formal way to structure (organize) some data (facts).

      => [8, 17, 42, 6]
      // OR
      a << 7
      => [8, 17, 42, 6, 7]


# dict style

# module style
puts MyStuff::TANGERINE

# class style
thing = MyStuff.new()
puts thing.tangerine


  • Class - tell Ruby to make a new type of thing.
  • Object - Two meanings: The most basic type of thing, and any instance of some thing.
  • Instance - What you get when you tell Ruby to create a class.
  • def - How to define a function inside a class.
  • @ Inside the functions in a class, self is a variable for the instance/object being accessed.
  • inheritance - the concept that one class can inherit traits from another class, much like you and your parents.
  • composition - The concept that a class can be composed of other classes as parts, much like how a car has wheels.
  • attribute - a property classes have that are from composition and are usually variables.
  • is-a - A phrase to say taht something inherits from another, as in a “salmon” is-a “fish”
  • has-a - a phrase to say that something is composed of other things or has a trait, as in “a salmon has-a mouth”

Ruby on Rails

  • DRY: Don’t repeat yourself.


MVC Framework

  • Controller: ActionController + ActionPack
  • View: ActionView + ActionPack
  • Model: ActiveRecord

Rails Framework

Rails Framework

  • if a static page exist it just returns whatever is in the public directory it does not reach-out to the Rails Framework unless it needs something rendered

Web Servers

  • Apache 1 or 2
    • Passenger / mod_rails
  • Nginx (“Engine X”)
  • Lighttpd
  • Mongrel
  • WEBrick

DB Severs

Built in support

  • sqlite3 (default)
  • mysql
  • postgresql

  • Creating entries in the DB with scaffolding
    • rails generate scaffold User name:string email:string
    • bundle exec rake db:migrate
      • created these columns in the DB, creates a Migrate file in DB folder

Random Commands

	// Generate a Controller
	rails generate controller StaticPages home help
	// Destroy the controller and it's pieces 
	rails destroy controller StaticPages home help

Random Code Bits

  • root_path gives you the path without a domain
  • root_url will give you the full url IE localhost:3000

Plugin support for

  • oracle
  • ibm_db
  • sqlserver
  • frontbase

Create a Site

rails new simple_cms -d mysql

# Start server 
rails server 

# or
rails s


  • Gemfile for all your custom gem-ys

      bundle install
  • Rake problems? Run.

      bundle exec rake db:migrate 

Routes (routes.rb)

  • Render top to bottom

Default Route

match ':controller(/:action(/:id))', :via => :get

// If you want to add a format IE .json
match ':controller(/:action(/:id(.:format)))', :via => :get


root "demo#index"


  • ERb “Embedded Ruby” is Ruby on Rails templating engine


<% code %>

// Outputs the results of the code
<%= code %>

// Get used to using hashes 
<%= link_to("Hello page 2", {:action => 'hello'}) %> 
  • a hash is multiple keys and values
  • keys (ruby symbols) and values

      // Single key & value
      b = { id: 3 }
      // Multiple keys & values
      b = { id: 3,
             status: "I just ate some delicious brains",
             zombie: "Jim }
      // Hash Recipe
      variable = { key: value }

Instance Variables

  • you can tell it’s an instance variable when it has an @ sign infront of it.
  • Instance variables are the only variables that need/have access to the templates.


Create a new model

rails generate model <model_name> table:type table:type
// Example 			 posts title:string link:string description:text

rake db:migrate

Remove a Model added without data or by accident

bundle exec rake db:rollback    
rails destroy model <model_name>

Rails Migrations

  • Set of DB instructions
  • “Migrate” your DB from one state to another
  • Contains instructions for both
    • Moving “up” to a new state
    • Moving back to a previous state
  • Allows sharing schema changes
  • Helps with vesioning
  • Allows writing Ruby instead of SQL

## Sinatra

  • My general rule of thumb is to use Rails when I need a database and Sinatra when I don’t.


  • http://blog.fil.vasilak.is/securing-the-sinatra-micro-framework/