ruby and rails by example (geekcamp edition)

Post on 15-May-2015

2.491 Views

Category:

Technology

6 Downloads

Preview:

Click to see full reader

DESCRIPTION

Old presentation updated for Geek Camp Baguio May 2012

TRANSCRIPT

Ruby and Railsby example

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

- Yukihiro "matz" Matsumoto,creator of Ruby

Example 0:Hash / Dictionary

// 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");

# Using Ruby

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

# Using Ruby 1.9

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

DO MOREwith

LESS CODE

// 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");

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=(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

Example 2.1:Traverse the Binary Tree

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

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

Example 3:Create a Person →

Student →College Student

class hierarchy

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"

Example 4:Call a method in a

"primitive"

nil.methods

true.object_id

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

Example 5:Find the sum of the

squares of all numbers under 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 employees

older 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 different

behavior for different instances

alice = Person.newbob = Person.new

alice.instance_eval do def hello puts "Hello" endend

def bob.hello puts "Howdy!"end

Example 10:Make Duck and

Person swim

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

class Duck include Swimmerend

class Person include Swimmerend

Duck.new.swimStudent.new.swim

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 RailsISN'T MAGIC

Ruby Features

DynamicObject Oriented

FunctionalMetaprogramming

+

Software Engineering

"Best Practices"

MVC CoC

DRY

TDD REST

= Productivity

= Magic?

DO MOREwith

LESS CODE

Rails Example:Demo a Twitter Clone

https://github.com/bryanbibat/microblog31

Authentication – DeviseAttachments – Paperclip

Pagination – KaminariTemplate Engine – HamlUI – Twitter Bootstrap

Ruby Resourcesmain site

http://www.ruby-lang.org

tutorialshttp://tryruby.org

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

Rails Resourcesmain site

http://rubyonrails.org/

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

Windows Installerhttp://railsinstaller.org/

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

top related