aitp ruby and rails talk

50
Ruby and Rails Jason Dew

Upload: jasondew

Post on 17-Jan-2015

1.279 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: AITP Ruby And Rails Talk

Ruby and RailsJason Dew

Page 2: AITP Ruby And Rails Talk

Ruby

Page 3: AITP Ruby And Rails Talk

pure OO

Page 4: AITP Ruby And Rails Talk

42.methods.sort

Page 5: AITP Ruby And Rails Talk

beautiful

Page 6: AITP Ruby And Rails Talk

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

Page 7: AITP Ruby And Rails Talk

Symbols

Page 8: AITP Ruby And Rails Talk

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

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

Page 9: AITP Ruby And Rails Talk

blocks

Page 10: AITP Ruby And Rails Talk

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

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

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

Page 11: AITP Ruby And Rails Talk

classes

Page 12: AITP Ruby And Rails Talk

class Monster def initialize @hitpoints = 400 end

def hitpoints @hitpoints end

end

dragon = Monster.newdragon.hitpoints # => 400

Page 13: AITP Ruby And Rails Talk

modules

Page 14: AITP Ruby And Rails Talk

module Bar def bar "baz" endend

include Barputs bar # => "baz"

Page 15: AITP Ruby And Rails Talk

open classes

Page 16: AITP Ruby And Rails Talk

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

Page 17: AITP Ruby And Rails Talk

define_method

Page 18: AITP Ruby And Rails Talk

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

Page 19: AITP Ruby And Rails Talk

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

Page 20: AITP Ruby And Rails Talk

method_missing

Page 21: AITP Ruby And Rails Talk

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"

Page 22: AITP Ruby And Rails Talk

singleton classes

Page 23: AITP Ruby And Rails Talk

class Barend

bar = Bar.new

def bar.bar "baz"end

bar.bar # => "baz"

Page 24: AITP Ruby And Rails Talk

Rails

Page 25: AITP Ruby And Rails Talk

RESTful routing

Page 26: AITP Ruby And Rails Talk

# config/routes.rb

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

Page 27: AITP Ruby And Rails Talk

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

Page 28: AITP Ruby And Rails Talk

HAML

Page 29: AITP Ruby And Rails Talk

# 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>

Page 30: AITP Ruby And Rails Talk

# HAML version!!! XML!!!

%html %head %title Page Title

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

Page 31: AITP Ruby And Rails Talk

ActiveRecord

Page 32: AITP Ruby And Rails Talk

migrations

Page 33: AITP Ruby And Rails Talk

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

Page 34: AITP Ruby And Rails Talk

associations

Page 35: AITP Ruby And Rails Talk

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

Page 36: AITP Ruby And Rails Talk

has many through

Page 37: AITP Ruby And Rails Talk

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

Page 38: AITP Ruby And Rails Talk

polymorphic models

Page 39: AITP Ruby And Rails Talk

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

Page 40: AITP Ruby And Rails Talk

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

Page 41: AITP Ruby And Rails Talk

plugins

Page 42: AITP Ruby And Rails Talk

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

http://railscasts.com/episodes/67

Page 43: AITP Ruby And Rails Talk

./script/generate authenticated user sessions

Page 44: AITP Ruby And Rails Talk

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

Page 45: AITP Ruby And Rails Talk

easy google maps integration

Page 46: AITP Ruby And Rails Talk

# 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

Page 47: AITP Ruby And Rails Talk

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

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

Page 48: AITP Ruby And Rails Talk

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

Page 49: AITP Ruby And Rails Talk

has_attachment :storage => :s3

Page 50: AITP Ruby And Rails Talk

SC Ruby ConferenceOctober 18th, 2008

Columbia, SC