aitp ruby and rails talk

Post on 17-Jan-2015

1.279 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Ruby and RailsJason Dew

Ruby

pure OO

42.methods.sort

beautiful

def fib n return 0 unless n > 0 return 1 if [1,2].include? n fib(n-1) + fib(n-2)end

puts fib(10) # => 55

Symbols

roygbiv = [:red, :orange, :yellow, :green, :blue, :indigo, :violet]

some_hash = { :bar => "bar", :life => 42 }

blocks

array = [42, "bar", ["x"]]

result = array.map do |element| element * 2end

puts result.inspect# => [84, "barbar", ["x", "x"]]

classes

class Monster def initialize @hitpoints = 400 end

def hitpoints @hitpoints end

end

dragon = Monster.newdragon.hitpoints # => 400

modules

module Bar def bar "baz" endend

include Barputs bar # => "baz"

open classes

class Array

def sum_of_squares inject(0) do |ss, element| ss + element ** 2 end end

end

[2,4,8,1,13,7].sum_of_squares # => 303

define_method

class Player

[:dexterity, :piety].each do |attribute| define_method(attribute) do puts "returning your #{attribute}" end

define_method("#{attribute}=") do |value| puts "setting #{attribute} to #{value}" end end

end

my = Player.newmy.dexterity # => returning your dexteritymy.piety = 3 # => setting piety to 3

method_missing

class Caster

def sissy_slap puts "your enemy laughs" end

def method_missing name, *args puts "#{name}: spell unknown" end

end

me = Caster.newme.sissy_slap # => "your enemy laughs"me.fireball # => "fireball: spell unknown"

singleton classes

class Barend

bar = Bar.new

def bar.bar "baz"end

bar.bar # => "baz"

Rails

RESTful routing

# config/routes.rb

ActionController::Routing::Routes.draw do |map| map.resources :barsend

Rails action Path method HTTP verb URL

index bars_path GET /bars

show bar_path(42) GET /bars/42

new new_bar_path GET /bars/new

create bars_path POST /bars

edit edit_bar_path(42) GET /bars/42/edit

update bar_path(42) PUT /bars/42

destroy bar_path(42) DELETE /bars/42

HAML

# RHTML version<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE html ...>

<html> <head> <title>Page Title</title> </head> <body> <h1>Page Header</h1> <div id="content"> <p class="news"> Some news content </p> </div> </body></html>

# HAML version!!! XML!!!

%html %head %title Page Title

%body %h1 Page Header #content %p.news Some news content

ActiveRecord

migrations

class CreateUsers < ActiveRecord::Migration def self.up create_table 'users' do |t| t.belongs_to :area t.string :login, :email, :name t.string :crypted_password, :salt t.timestamps end end

def self.down drop_table 'users' endend

associations

class Team < ActiveRecord::Base has_many :playersend

class Player < ActiveRecord::Base belongs_to :teamend

gamecocks = Team.find_by_name("Gamecocks")gamecocks.players # => returns player array

has many through

class Reader < ActiveRecord::Base has_many :feeds, :through => :subscriptionsend

class Feed < ActiveRecord::Base has_many :readers, :through => :subscriptionsend

class Subscription < ActiveRecord::Base belongs_to :reader belongs_to :feedend

ars_technica = Feed.find(42)ars_technica.readers # => some large integer

polymorphic models

class Client < ActiveRecord::Base has_many :addreses, :as => :addressableend

class Provider < ActiveRecord::Base has_many :addresses, :as => :addressableend

class Address < ActiveRecord::Base belongs_to :addressable, :polymorphic => trueend

Client.first.addresses.build Address.newProvider.first.addresses.build Address.new

# migrationcreate_table :addresses do |t| t.string :street, :city, :zip_code t.references :addressable, :polymorphic => true # ^^ creates addressable_id # and addressable_typeend

plugins

restful_authenticationhttp://github.com/technoweenie/restful-authentication/tree/master

http://railscasts.com/episodes/67

./script/generate authenticated user sessions

ym4r-gmhttp://github.com/bitbckt/ym4r-gm/tree/master

easy google maps integration

# controller def index @map = GMap.new "map_div" @map.control_init :large_map => true, :map_type => true @map.center_zoom_init [75.5, -42.56], 4

marker = GMarker.new [75.6, -42.467], :title => "title", :info_window => "bar" @map.overlay_init marker end

# view<html> <head> <%= GMap.header %> <%= @map.to_html %> </head>

<body> <%= @map.div %> </body></html>

attachment_fuhttp://github.com/technoweenie/attachment_fu/tree/master

has_attachment :storage => :s3

SC Ruby ConferenceOctober 18th, 2008

Columbia, SC

top related