ruby and rails by example (geekcamp edition)
DESCRIPTION
Old presentation updated for Geek Camp Baguio May 2012TRANSCRIPT
![Page 1: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/1.jpg)
Ruby and Railsby example
![Page 2: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/2.jpg)
![Page 3: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/3.jpg)
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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/4.jpg)
Example 0:Hash / Dictionary
![Page 5: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/5.jpg)
// 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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/6.jpg)
# Using Ruby
openWith = { "txt" => "notepad.exe", "bmp" => "paint.exe", "dib" => "paint.exe", "rtf" => "wordpad.exe" }
![Page 7: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/7.jpg)
# 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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/8.jpg)
![Page 9: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/9.jpg)
DO MOREwith
LESS CODE
![Page 10: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/10.jpg)
// 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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/11.jpg)
Example 1:Hello World
![Page 12: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/12.jpg)
puts "Hello World!"
![Page 13: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/13.jpg)
Example 2:Create a Binary Tree
![Page 14: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/14.jpg)
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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/15.jpg)
Example 2.1:Traverse the Binary Tree
![Page 16: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/16.jpg)
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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/17.jpg)
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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/18.jpg)
Example 3:Create a Person →
Student →College Student
class hierarchy
![Page 19: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/19.jpg)
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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/20.jpg)
Example 4:Call a method in a
"primitive"
![Page 21: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/21.jpg)
nil.methods
true.object_id
1.upto(10) do |x| puts xend
![Page 22: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/22.jpg)
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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/23.jpg)
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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/24.jpg)
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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/25.jpg)
Example 6:Find all employees
older than 30 and sort by last name
![Page 26: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/26.jpg)
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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/27.jpg)
Example 7:Assign a method to a
variable
![Page 28: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/28.jpg)
hello = Proc.new { |string| puts "Hello #{string}" }
hello.call "Alice"
![Page 29: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/29.jpg)
Example 8:Add a "plus" method to
all numbers
![Page 30: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/30.jpg)
class Numeric def plus(value) self.+(value) endend
![Page 31: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/31.jpg)
Example 9:Define different
behavior for different instances
![Page 32: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/32.jpg)
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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/33.jpg)
Example 10:Make Duck and
Person swim
![Page 34: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/34.jpg)
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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/35.jpg)
![Page 36: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/36.jpg)
![Page 37: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/37.jpg)
Example 0:Make a Twitter Clone
![Page 38: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/38.jpg)
$ 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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/39.jpg)
$ 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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/40.jpg)
Ruby on RailsISN'T MAGIC
![Page 41: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/41.jpg)
![Page 42: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/42.jpg)
Ruby Features
![Page 43: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/43.jpg)
DynamicObject Oriented
FunctionalMetaprogramming
![Page 44: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/44.jpg)
+
![Page 45: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/45.jpg)
Software Engineering
"Best Practices"
![Page 46: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/46.jpg)
MVC CoC
DRY
TDD REST
![Page 47: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/47.jpg)
= Productivity
![Page 48: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/48.jpg)
= Magic?
![Page 49: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/49.jpg)
DO MOREwith
LESS CODE
![Page 50: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/50.jpg)
Rails Example:Demo a Twitter Clone
![Page 51: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/51.jpg)
https://github.com/bryanbibat/microblog31
Authentication – DeviseAttachments – Paperclip
Pagination – KaminariTemplate Engine – HamlUI – Twitter Bootstrap
![Page 52: Ruby and Rails by Example (GeekCamp edition)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/52.jpg)
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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/53.jpg)
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)](https://reader033.vdocuments.us/reader033/viewer/2022052410/5555a991d8b42a52568b484e/html5/thumbnails/54.jpg)
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