ruby and rails by example (geekcamp edition)

54
Ruby and Rails by example

Upload: bryanbibat

Post on 15-May-2015

2.491 views

Category:

Technology


6 download

DESCRIPTION

Old presentation updated for Geek Camp Baguio May 2012

TRANSCRIPT

Page 1: Ruby and Rails by Example (GeekCamp edition)

Ruby and Railsby example

Page 2: Ruby and Rails by Example (GeekCamp edition)
Page 3: Ruby and Rails by Example (GeekCamp edition)

Ruby is simple in appearance,but is very complex inside,just like our human body.

- Yukihiro "matz" Matsumoto,creator of Ruby

Page 4: Ruby and Rails by Example (GeekCamp edition)

Example 0:Hash / Dictionary

Page 5: Ruby and Rails by Example (GeekCamp edition)

// Using C#

using System;using System.Collections;

...

Hashtable openWith = new Hashtable();

openWith.Add("txt", "notepad.exe"); openWith.Add("bmp", "paint.exe"); openWith.Add("dib", "paint.exe"); openWith.Add("rtf", "wordpad.exe");

Page 6: Ruby and Rails by Example (GeekCamp edition)

# Using Ruby

openWith = { "txt" => "notepad.exe", "bmp" => "paint.exe", "dib" => "paint.exe", "rtf" => "wordpad.exe" }

Page 7: Ruby and Rails by Example (GeekCamp edition)

# Using Ruby 1.9

openWith = { txt: "notepad.exe", bmp: "paint.exe", dib: "paint.exe", rtf: "wordpad.exe" }

Page 8: Ruby and Rails by Example (GeekCamp edition)
Page 9: Ruby and Rails by Example (GeekCamp edition)

DO MOREwith

LESS CODE

Page 10: Ruby and Rails by Example (GeekCamp edition)

// Using C#

using System;using System.Collections;

...

Hashtable openWith = new Hashtable();

openWith.Add("txt", "notepad.exe"); openWith.Add("bmp", "paint.exe"); openWith.Add("dib", "paint.exe"); openWith.Add("rtf", "wordpad.exe");

Page 11: Ruby and Rails by Example (GeekCamp edition)

Example 1:Hello World

Page 12: Ruby and Rails by Example (GeekCamp edition)

puts "Hello World!"

Page 13: Ruby and Rails by Example (GeekCamp edition)

Example 2:Create a Binary Tree

Page 14: Ruby and Rails by Example (GeekCamp edition)

class Node attr_accessor :value

def initialize(value = nil) @value = value end

attr_reader :left, :right def left=(node); @left = create_node(node); end def right=(node); @right = create_node(node); end

private def create_node(node) node.instance_of? Node ? node : Node.new(node) endend

Page 15: Ruby and Rails by Example (GeekCamp edition)

Example 2.1:Traverse the Binary Tree

Page 16: Ruby and Rails by Example (GeekCamp edition)

def traverse(node) visited_list = [] inorder(node, visited) puts visited.join(",")end

def inorder(node, visited) inorder(node.left, visited) unless node.left.nil? visited << node.value inorder(node.right, visited) unless node.right.nil?end

Page 17: Ruby and Rails by Example (GeekCamp edition)

def traverse(node) visited_list = [] inorder node, visited puts visited.join ","end

def inorder(node, visited) inorder node.left, visited unless node.left.nil? visited << node.value inorder node.right, visited unless node.right.nil?end

Page 18: Ruby and Rails by Example (GeekCamp edition)

Example 3:Create a Person →

Student →College Student

class hierarchy

Page 19: Ruby and Rails by Example (GeekCamp edition)

class Person attr_accessor :nameend

class Student < Person attr_accessor :schoolend

class CollegeStudent < Student attr_accessor :courseend

x = CollegeStudent.newx.name = "John Doe"x.school = "ABC University"x.course = "Computer Science"

Page 20: Ruby and Rails by Example (GeekCamp edition)

Example 4:Call a method in a

"primitive"

Page 21: Ruby and Rails by Example (GeekCamp edition)

nil.methods

true.object_id

1.upto(10) do |x| puts xend

Page 22: Ruby and Rails by Example (GeekCamp edition)

Example 5:Find the sum of the

squares of all numbers under 10,000 divisible

by 3 and/or 5

Page 23: Ruby and Rails by Example (GeekCamp edition)

x = 1sum = 0while x < 10000 do if x % 3 == 0 or x % 5 == 0 sum += x * x endendputs sum

Page 24: Ruby and Rails by Example (GeekCamp edition)

puts (1..10000). select { |x| x % 3 == 0 or x % 5 == 0}. map {|x| x * x }. reduce(:+)

Page 25: Ruby and Rails by Example (GeekCamp edition)

Example 6:Find all employees

older than 30 and sort by last name

Page 26: Ruby and Rails by Example (GeekCamp edition)

oldies = employees.select { |e| e.age > 30 }. sort { |e1, e2| e1.last_name <=> e2.last_name }

Page 27: Ruby and Rails by Example (GeekCamp edition)

Example 7:Assign a method to a

variable

Page 28: Ruby and Rails by Example (GeekCamp edition)

hello = Proc.new { |string| puts "Hello #{string}" }

hello.call "Alice"

Page 29: Ruby and Rails by Example (GeekCamp edition)

Example 8:Add a "plus" method to

all numbers

Page 30: Ruby and Rails by Example (GeekCamp edition)

class Numeric def plus(value) self.+(value) endend

Page 31: Ruby and Rails by Example (GeekCamp edition)

Example 9:Define different

behavior for different instances

Page 32: Ruby and Rails by Example (GeekCamp edition)

alice = Person.newbob = Person.new

alice.instance_eval do def hello puts "Hello" endend

def bob.hello puts "Howdy!"end

Page 33: Ruby and Rails by Example (GeekCamp edition)

Example 10:Make Duck and

Person swim

Page 34: Ruby and Rails by Example (GeekCamp edition)

module Swimmer def swim puts "This #{self.class} is swimming" endend

class Duck include Swimmerend

class Person include Swimmerend

Duck.new.swimStudent.new.swim

Page 35: Ruby and Rails by Example (GeekCamp edition)
Page 36: Ruby and Rails by Example (GeekCamp edition)
Page 37: Ruby and Rails by Example (GeekCamp edition)

Example 0:Make a Twitter Clone

Page 38: Ruby and Rails by Example (GeekCamp edition)

$ rails new twitclone$ cd twitclone$ rails generate scaffold tweet message:string$ rake db:migrate $ rails server

Page 39: Ruby and Rails by Example (GeekCamp edition)

$ rails new twitclone$ cd twitclone$ rails generate scaffold tweet message:string$ rake db:migrate $ rails server

Page 40: Ruby and Rails by Example (GeekCamp edition)

Ruby on RailsISN'T MAGIC

Page 41: Ruby and Rails by Example (GeekCamp edition)
Page 42: Ruby and Rails by Example (GeekCamp edition)

Ruby Features

Page 43: Ruby and Rails by Example (GeekCamp edition)

DynamicObject Oriented

FunctionalMetaprogramming

Page 44: Ruby and Rails by Example (GeekCamp edition)

+

Page 45: Ruby and Rails by Example (GeekCamp edition)

Software Engineering

"Best Practices"

Page 46: Ruby and Rails by Example (GeekCamp edition)

MVC CoC

DRY

TDD REST

Page 47: Ruby and Rails by Example (GeekCamp edition)

= Productivity

Page 48: Ruby and Rails by Example (GeekCamp edition)

= Magic?

Page 49: Ruby and Rails by Example (GeekCamp edition)

DO MOREwith

LESS CODE

Page 50: Ruby and Rails by Example (GeekCamp edition)

Rails Example:Demo a Twitter Clone

Page 51: Ruby and Rails by Example (GeekCamp edition)

https://github.com/bryanbibat/microblog31

Authentication – DeviseAttachments – Paperclip

Pagination – KaminariTemplate Engine – HamlUI – Twitter Bootstrap

Page 52: Ruby and Rails by Example (GeekCamp edition)

Ruby Resourcesmain site

http://www.ruby-lang.org

tutorialshttp://tryruby.org

http://ruby.learncodethehardway.org/http://mislav.uniqpath.com/poignant-guide/

Page 53: Ruby and Rails by Example (GeekCamp edition)

Rails Resourcesmain site

http://rubyonrails.org/

tutorialshttp://ruby.railstutorial.org/http://railsforzombies.org/

Windows Installerhttp://railsinstaller.org/

Page 54: Ruby and Rails by Example (GeekCamp edition)

Thank You For Listening!

Philippine Ruby Users Group:http://pinoyrb.org

https://groups.google.com/forum/#!forum/ruby-phil

me: http://bryanbibat.net | @bry_bibat