Successfully reported this slideshow.
Ruby and Rails   by example
Ruby is simple in appearance, but is very complex inside,  just like our human body.         - Yukihiro "matz" Matsumoto, ...
Example 0:Hash / Dictionary
// Using C#using System;using System.Collections;...        Hashtable openWith = new Hashtable();        openWith.Add("txt...
# Using RubyopenWith = { "txt"   =>   "notepad.exe",             "bmp"   =>   "paint.exe",             "dib"   =>   "paint...
# Using Ruby 1.9openWith = { txt: "notepad.exe",  bmp: "paint.exe", dib: "paint.exe",  rtf: "wordpad.exe" }
DO MORE   withLESS CODE
// Using C#using System;using System.Collections;...        Hashtable openWith = new Hashtable();        openWith.Add("txt...
Example 1:Hello World
puts "Hello World!"
Example 2:Create a Binary Tree
class Node  attr_accessor :value  def initialize(value = nil)    @value = value  end  attr_reader :left, :right  def left=...
Example 2.1:Traverse the Binary Tree
def traverse(node)  visited_list = []  inorder(node, visited)  puts visited.join(",")enddef inorder(node, visited)  inorde...
def traverse(node)  visited_list = []  inorder node, visited  puts visited.join ","enddef inorder(node, visited)  inorder ...
Example 3:Create a Person →   Student →College Student class hierarchy
class Person  attr_accessor :nameendclass Student < Person  attr_accessor :schoolendclass CollegeStudent < Student  attr_a...
Example 4:Call a method in a    "primitive"
nil.methodstrue.object_id1.upto(10) do |x|  puts xend
Example 5: Find the sum of thesquares of all numbersunder 10,000 divisible    by 3 and/or 5
x = 1sum = 0while x < 10000 do  if x % 3 == 0 or x % 5 == 0    sum += x * x  endendputs sum
puts (1..10000).  select { |x| x % 3 == 0 or x % 5 == 0}.  map {|x| x * x }.  reduce(:+)
Example 6:  Find all employeesolder than 30 and sort     by last name
oldies = employees.select { |e| e.age > 30 }.  sort { |e1, e2| e1.last_name <=> e2.last_name }
Example 7:Assign a method to a      variable
hello = Proc.new { |string| puts "Hello #{string}" }hello.call "Alice"
Example 8:Add a "plus" method to     all numbers
class Numeric  def plus(value)    self.+(value)  endend
Example 9:  Define differentbehavior for different     instances
alice = Person.newbob = Person.newalice.instance_eval do  def hello    puts "Hello"  endenddef bob.hello  puts "Howdy!"end
Example 10:Make Duck and Person swim
module Swimmer  def swim    puts "This #{self.class} is swimming"  endendclass Duck  include Swimmerendclass Person  inclu...
Example 0:Make a Twitter Clone
$   rails new twitclone$   cd twitclone$   rails generate scaffold tweet message:string$   rake db:migrate$   rails server
$   rails new twitclone$   cd twitclone$   rails generate scaffold tweet message:string$   rake db:migrate$   rails server
Ruby on RailsISNT MAGIC
Ruby Features
Dynamic Object Oriented   FunctionalMetaprogramming
+
Software  Engineering"Best Practices"
MVC   CoC  DRYTDD   REST
= Productivity
= Magic?
DO MORE   withLESS CODE
Rails Example:Demo a Twitter Clone
https://github.com/bryanbibat/microblog31       Authentication – Devise       Attachments – Paperclip        Pagination – ...
Bonus Example:  Scale the Twitter Clone
Thank You For  Listening!Philippine Ruby Users Group:      http://pinoyrb.orgme: http://bryanbibat.net | @bry_bibat
Ruby and Rails by example
Ruby and Rails by example
Ruby and Rails by example
Ruby and Rails by example
Ruby and Rails by example
Upcoming SlideShare
Loading in …5
×

Ruby and Rails by example

34,608 views

Published on

My slides for DevCon @ Y4iT 2011 lightning talk

Published in: Technology

Ruby and Rails by example

  1. 1. Ruby and Rails by example
  2. 2. Ruby is simple in appearance, but is very complex inside, just like our human body. - Yukihiro "matz" Matsumoto, creator of Ruby
  3. 3. Example 0:Hash / Dictionary
  4. 4. // 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");
  5. 5. # Using RubyopenWith = { "txt" => "notepad.exe", "bmp" => "paint.exe", "dib" => "paint.exe", "rtf" => "wordpad.exe" }
  6. 6. # Using Ruby 1.9openWith = { txt: "notepad.exe", bmp: "paint.exe", dib: "paint.exe", rtf: "wordpad.exe" }
  7. 7. DO MORE withLESS CODE
  8. 8. // 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");
  9. 9. Example 1:Hello World
  10. 10. puts "Hello World!"
  11. 11. Example 2:Create a Binary Tree
  12. 12. 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
  13. 13. Example 2.1:Traverse the Binary Tree
  14. 14. def traverse(node) visited_list = [] inorder(node, visited) puts visited.join(",")enddef inorder(node, visited) inorder(node.left, visited) unless node.left.nil? visited << node.value inorder(node.right, visited) unless node.right.nil?end
  15. 15. def traverse(node) visited_list = [] inorder node, visited puts visited.join ","enddef inorder(node, visited) inorder node.left, visited unless node.left.nil? visited << node.value inorder node.right, visited unless node.right.nil?end
  16. 16. Example 3:Create a Person → Student →College Student class hierarchy
  17. 17. class Person attr_accessor :nameendclass Student < Person attr_accessor :schoolendclass CollegeStudent < Student attr_accessor :courseendx = CollegeStudent.newx.name = "John Doe"x.school = "ABC University"x.course = "Computer Science"
  18. 18. Example 4:Call a method in a "primitive"
  19. 19. nil.methodstrue.object_id1.upto(10) do |x| puts xend
  20. 20. Example 5: Find the sum of thesquares of all numbersunder 10,000 divisible by 3 and/or 5
  21. 21. x = 1sum = 0while x < 10000 do if x % 3 == 0 or x % 5 == 0 sum += x * x endendputs sum
  22. 22. puts (1..10000). select { |x| x % 3 == 0 or x % 5 == 0}. map {|x| x * x }. reduce(:+)
  23. 23. Example 6: Find all employeesolder than 30 and sort by last name
  24. 24. oldies = employees.select { |e| e.age > 30 }. sort { |e1, e2| e1.last_name <=> e2.last_name }
  25. 25. Example 7:Assign a method to a variable
  26. 26. hello = Proc.new { |string| puts "Hello #{string}" }hello.call "Alice"
  27. 27. Example 8:Add a "plus" method to all numbers
  28. 28. class Numeric def plus(value) self.+(value) endend
  29. 29. Example 9: Define differentbehavior for different instances
  30. 30. alice = Person.newbob = Person.newalice.instance_eval do def hello puts "Hello" endenddef bob.hello puts "Howdy!"end
  31. 31. Example 10:Make Duck and Person swim
  32. 32. module Swimmer def swim puts "This #{self.class} is swimming" endendclass Duck include Swimmerendclass Person include SwimmerendDuck.new.swimStudent.new.swim
  33. 33. Example 0:Make a Twitter Clone
  34. 34. $ rails new twitclone$ cd twitclone$ rails generate scaffold tweet message:string$ rake db:migrate$ rails server
  35. 35. $ rails new twitclone$ cd twitclone$ rails generate scaffold tweet message:string$ rake db:migrate$ rails server
  36. 36. Ruby on RailsISNT MAGIC
  37. 37. Ruby Features
  38. 38. Dynamic Object Oriented FunctionalMetaprogramming
  39. 39. +
  40. 40. Software Engineering"Best Practices"
  41. 41. MVC CoC DRYTDD REST
  42. 42. = Productivity
  43. 43. = Magic?
  44. 44. DO MORE withLESS CODE
  45. 45. Rails Example:Demo a Twitter Clone
  46. 46. https://github.com/bryanbibat/microblog31 Authentication – Devise Attachments – Paperclip Pagination – Kaminari Template Engine – Haml UI – Twitter Bootstrap
  47. 47. Bonus Example: Scale the Twitter Clone
  48. 48. Thank You For Listening!Philippine Ruby Users Group: http://pinoyrb.orgme: http://bryanbibat.net | @bry_bibat

×