SlideShare a Scribd company logo
1 of 54
Download to read offline
Ruby and Rails
   by 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 MORE
   with

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)
  end
end
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 :name
end

class Student < Person
  attr_accessor :school
end

class CollegeStudent < Student
  attr_accessor :course
end

x = CollegeStudent.new
x.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 x
end
Example 5:
 Find the sum of the
squares of all numbers
under 10,000 divisible
    by 3 and/or 5
x = 1
sum = 0
while x < 10000 do
  if x % 3 == 0 or x % 5 == 0
    sum += x * x
  end
end
puts 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)
  end
end
Example 9:
  Define different
behavior for different
     instances
alice = Person.new
bob = Person.new

alice.instance_eval do
  def hello
    puts "Hello"
  end
end

def bob.hello
  puts "Howdy!"
end
Example 10:
Make Duck and
 Person swim
module Swimmer
  def swim
    puts "This #{self.class} is swimming"
  end
end

class Duck
  include Swimmer
end

class Person
  include Swimmer
end

Duck.new.swim
Student.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 Rails
ISN'T MAGIC
Ruby Features
Dynamic
 Object Oriented
   Functional
Metaprogramming
+
Software
  Engineering
"Best Practices"
MVC   CoC
  DRY
TDD   REST
= Productivity
= Magic?
DO MORE
   with

LESS CODE
Rails Example:
Demo a Twitter Clone
https://github.com/bryanbibat/microblog31

       Authentication – Devise
       Attachments – Paperclip
        Pagination – Kaminari
       Template Engine – Haml
        UI – Twitter Bootstrap
Ruby Resources
               main site
       http://www.ruby-lang.org

               tutorials
          http://tryruby.org
http://ruby.learncodethehardway.org/
http://mislav.uniqpath.com/poignant-guide/
Rails Resources
          main site
   http://rubyonrails.org/

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

     Windows Installer
   http://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

More Related Content

What's hot

Fast Slim Correct: The History and Evolution of JavaScript.
Fast Slim Correct: The History and Evolution of JavaScript.Fast Slim Correct: The History and Evolution of JavaScript.
Fast Slim Correct: The History and Evolution of JavaScript.John Dalziel
 
Webpack & EcmaScript 6 (Webelement #32)
Webpack & EcmaScript 6 (Webelement #32)Webpack & EcmaScript 6 (Webelement #32)
Webpack & EcmaScript 6 (Webelement #32)srigi
 
JavaScript Performance (at SFJS)
JavaScript Performance (at SFJS)JavaScript Performance (at SFJS)
JavaScript Performance (at SFJS)Steve Souders
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Railsmithunsasidharan
 
I18nize Scala programs à la gettext
I18nize Scala programs à la gettextI18nize Scala programs à la gettext
I18nize Scala programs à la gettextNgoc Dao
 
Web application intro + a bit of ruby (revised)
Web application intro + a bit of ruby (revised)Web application intro + a bit of ruby (revised)
Web application intro + a bit of ruby (revised)Tobias Pfeiffer
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumNgoc Dao
 
Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Mark Menard
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on RailsAlessandro DS
 
Building Isomorphic Apps (JSConf.Asia 2014)
Building Isomorphic Apps (JSConf.Asia 2014)Building Isomorphic Apps (JSConf.Asia 2014)
Building Isomorphic Apps (JSConf.Asia 2014)Spike Brehm
 
Web development basics (Part-3)
Web development basics (Part-3)Web development basics (Part-3)
Web development basics (Part-3)Rajat Pratap Singh
 
Module design pattern i.e. express js
Module design pattern i.e. express jsModule design pattern i.e. express js
Module design pattern i.e. express jsAhmed Assaf
 
Introduction to JRuby
Introduction to JRubyIntroduction to JRuby
Introduction to JRubyelliando dias
 
Unobtrusive JavaScript
Unobtrusive JavaScriptUnobtrusive JavaScript
Unobtrusive JavaScriptdaveverwer
 
Ruby an overall approach
Ruby an overall approachRuby an overall approach
Ruby an overall approachFelipe Schmitt
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsMike Subelsky
 
Security Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSecurity Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSource Conference
 
Frozen rails 2012 - Fighting Code Smells
Frozen rails 2012 - Fighting Code SmellsFrozen rails 2012 - Fighting Code Smells
Frozen rails 2012 - Fighting Code SmellsDennis Ushakov
 

What's hot (20)

Fast Slim Correct: The History and Evolution of JavaScript.
Fast Slim Correct: The History and Evolution of JavaScript.Fast Slim Correct: The History and Evolution of JavaScript.
Fast Slim Correct: The History and Evolution of JavaScript.
 
Webpack & EcmaScript 6 (Webelement #32)
Webpack & EcmaScript 6 (Webelement #32)Webpack & EcmaScript 6 (Webelement #32)
Webpack & EcmaScript 6 (Webelement #32)
 
JavaScript Performance (at SFJS)
JavaScript Performance (at SFJS)JavaScript Performance (at SFJS)
JavaScript Performance (at SFJS)
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
I18nize Scala programs à la gettext
I18nize Scala programs à la gettextI18nize Scala programs à la gettext
I18nize Scala programs à la gettext
 
Web application intro + a bit of ruby (revised)
Web application intro + a bit of ruby (revised)Web application intro + a bit of ruby (revised)
Web application intro + a bit of ruby (revised)
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and Xitrum
 
Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1
 
Road to Rails
Road to RailsRoad to Rails
Road to Rails
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Building Isomorphic Apps (JSConf.Asia 2014)
Building Isomorphic Apps (JSConf.Asia 2014)Building Isomorphic Apps (JSConf.Asia 2014)
Building Isomorphic Apps (JSConf.Asia 2014)
 
Web development basics (Part-3)
Web development basics (Part-3)Web development basics (Part-3)
Web development basics (Part-3)
 
Module design pattern i.e. express js
Module design pattern i.e. express jsModule design pattern i.e. express js
Module design pattern i.e. express js
 
Introduction to JRuby
Introduction to JRubyIntroduction to JRuby
Introduction to JRuby
 
Unobtrusive JavaScript
Unobtrusive JavaScriptUnobtrusive JavaScript
Unobtrusive JavaScript
 
Ruby an overall approach
Ruby an overall approachRuby an overall approach
Ruby an overall approach
 
Why Use Rails by Dr Nic
Why Use Rails by  Dr NicWhy Use Rails by  Dr Nic
Why Use Rails by Dr Nic
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
 
Security Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSecurity Goodness with Ruby on Rails
Security Goodness with Ruby on Rails
 
Frozen rails 2012 - Fighting Code Smells
Frozen rails 2012 - Fighting Code SmellsFrozen rails 2012 - Fighting Code Smells
Frozen rails 2012 - Fighting Code Smells
 

Similar to Ruby and Rails by Example (GeekCamp edition)

Ruby and Rails by example
Ruby and Rails by exampleRuby and Rails by example
Ruby and Rails by examplebryanbibat
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends旻琦 潘
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme SwiftMovel
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Railsrstankov
 
Ruby on Rails 3.1: Let's bring the fun back into web programing
Ruby on Rails 3.1: Let's bring the fun back into web programingRuby on Rails 3.1: Let's bring the fun back into web programing
Ruby on Rails 3.1: Let's bring the fun back into web programingBozhidar Batsov
 
Introduction to ReasonML
Introduction to ReasonMLIntroduction to ReasonML
Introduction to ReasonMLRiza Fahmi
 
Uses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsUses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsPatchSpace Ltd
 
Container (Docker) Orchestration Tools
Container (Docker) Orchestration ToolsContainer (Docker) Orchestration Tools
Container (Docker) Orchestration ToolsDhilipsiva DS
 
Perkenalan ReasonML
Perkenalan ReasonMLPerkenalan ReasonML
Perkenalan ReasonMLRiza Fahmi
 
Snakes for Camels
Snakes for CamelsSnakes for Camels
Snakes for Camelsmiquelruizm
 
Desarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosDesarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosEdgar Suarez
 
Reasons To Love Ruby
Reasons To Love RubyReasons To Love Ruby
Reasons To Love RubyBen Scheirman
 
Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Pedro Cunha
 

Similar to Ruby and Rails by Example (GeekCamp edition) (20)

Ruby and Rails by example
Ruby and Rails by exampleRuby and Rails by example
Ruby and Rails by example
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 
Ruby gems
Ruby gemsRuby gems
Ruby gems
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
Ruby on Rails 3.1: Let's bring the fun back into web programing
Ruby on Rails 3.1: Let's bring the fun back into web programingRuby on Rails 3.1: Let's bring the fun back into web programing
Ruby on Rails 3.1: Let's bring the fun back into web programing
 
Introduction to ReasonML
Introduction to ReasonMLIntroduction to ReasonML
Introduction to ReasonML
 
Uses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsUses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & Stubs
 
Container (Docker) Orchestration Tools
Container (Docker) Orchestration ToolsContainer (Docker) Orchestration Tools
Container (Docker) Orchestration Tools
 
CppTutorial.ppt
CppTutorial.pptCppTutorial.ppt
CppTutorial.ppt
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Perkenalan ReasonML
Perkenalan ReasonMLPerkenalan ReasonML
Perkenalan ReasonML
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 
Snakes for Camels
Snakes for CamelsSnakes for Camels
Snakes for Camels
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Desarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosDesarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutos
 
Reasons To Love Ruby
Reasons To Love RubyReasons To Love Ruby
Reasons To Love Ruby
 
Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11
 

More from bryanbibat

Static Sites in Ruby
Static Sites in RubyStatic Sites in Ruby
Static Sites in Rubybryanbibat
 
So You Want to Teach Ruby and Rails...
So You Want to Teach Ruby and Rails...So You Want to Teach Ruby and Rails...
So You Want to Teach Ruby and Rails...bryanbibat
 
Git Basics (Professionals)
 Git Basics (Professionals) Git Basics (Professionals)
Git Basics (Professionals)bryanbibat
 
Upgrading to Ruby 2.1, Rails 4.0, Bootstrap 3.0
Upgrading to Ruby 2.1, Rails 4.0, Bootstrap 3.0Upgrading to Ruby 2.1, Rails 4.0, Bootstrap 3.0
Upgrading to Ruby 2.1, Rails 4.0, Bootstrap 3.0bryanbibat
 
Version Control with Git for Beginners
Version Control with Git for BeginnersVersion Control with Git for Beginners
Version Control with Git for Beginnersbryanbibat
 
Rails is Easy*
Rails is Easy*Rails is Easy*
Rails is Easy*bryanbibat
 
Things Future IT Students Should Know (But Don't)
Things Future IT Students Should Know (But Don't)Things Future IT Students Should Know (But Don't)
Things Future IT Students Should Know (But Don't)bryanbibat
 
Things IT Undergrads Should Know (But Don't)
Things IT Undergrads Should Know (But Don't)Things IT Undergrads Should Know (But Don't)
Things IT Undergrads Should Know (But Don't)bryanbibat
 
From Novice to Expert: A Pragmatic Approach to Learning
From Novice to Expert: A Pragmatic Approach to LearningFrom Novice to Expert: A Pragmatic Approach to Learning
From Novice to Expert: A Pragmatic Approach to Learningbryanbibat
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8bryanbibat
 
Preparing for the WebGeek DevCup
Preparing for the WebGeek DevCupPreparing for the WebGeek DevCup
Preparing for the WebGeek DevCupbryanbibat
 
Productive text editing with Vim
Productive text editing with VimProductive text editing with Vim
Productive text editing with Vimbryanbibat
 
Latest Trends in Web Technologies
Latest Trends in Web TechnologiesLatest Trends in Web Technologies
Latest Trends in Web Technologiesbryanbibat
 
Virtualization
VirtualizationVirtualization
Virtualizationbryanbibat
 
Some Myths in Software Development
Some Myths in Software DevelopmentSome Myths in Software Development
Some Myths in Software Developmentbryanbibat
 
Latest Trends in Open Source Web Technologies
Latest Trends in Open Source Web TechnologiesLatest Trends in Open Source Web Technologies
Latest Trends in Open Source Web Technologiesbryanbibat
 
What it takes to be a Web Developer
What it takes to be a Web DeveloperWhat it takes to be a Web Developer
What it takes to be a Web Developerbryanbibat
 
before you leap
before you leapbefore you leap
before you leapbryanbibat
 
Sowing the Seeds
Sowing the SeedsSowing the Seeds
Sowing the Seedsbryanbibat
 

More from bryanbibat (20)

Hd 10 japan
Hd 10 japanHd 10 japan
Hd 10 japan
 
Static Sites in Ruby
Static Sites in RubyStatic Sites in Ruby
Static Sites in Ruby
 
So You Want to Teach Ruby and Rails...
So You Want to Teach Ruby and Rails...So You Want to Teach Ruby and Rails...
So You Want to Teach Ruby and Rails...
 
Git Basics (Professionals)
 Git Basics (Professionals) Git Basics (Professionals)
Git Basics (Professionals)
 
Upgrading to Ruby 2.1, Rails 4.0, Bootstrap 3.0
Upgrading to Ruby 2.1, Rails 4.0, Bootstrap 3.0Upgrading to Ruby 2.1, Rails 4.0, Bootstrap 3.0
Upgrading to Ruby 2.1, Rails 4.0, Bootstrap 3.0
 
Version Control with Git for Beginners
Version Control with Git for BeginnersVersion Control with Git for Beginners
Version Control with Git for Beginners
 
Rails is Easy*
Rails is Easy*Rails is Easy*
Rails is Easy*
 
Things Future IT Students Should Know (But Don't)
Things Future IT Students Should Know (But Don't)Things Future IT Students Should Know (But Don't)
Things Future IT Students Should Know (But Don't)
 
Things IT Undergrads Should Know (But Don't)
Things IT Undergrads Should Know (But Don't)Things IT Undergrads Should Know (But Don't)
Things IT Undergrads Should Know (But Don't)
 
From Novice to Expert: A Pragmatic Approach to Learning
From Novice to Expert: A Pragmatic Approach to LearningFrom Novice to Expert: A Pragmatic Approach to Learning
From Novice to Expert: A Pragmatic Approach to Learning
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
Preparing for the WebGeek DevCup
Preparing for the WebGeek DevCupPreparing for the WebGeek DevCup
Preparing for the WebGeek DevCup
 
Productive text editing with Vim
Productive text editing with VimProductive text editing with Vim
Productive text editing with Vim
 
Latest Trends in Web Technologies
Latest Trends in Web TechnologiesLatest Trends in Web Technologies
Latest Trends in Web Technologies
 
Virtualization
VirtualizationVirtualization
Virtualization
 
Some Myths in Software Development
Some Myths in Software DevelopmentSome Myths in Software Development
Some Myths in Software Development
 
Latest Trends in Open Source Web Technologies
Latest Trends in Open Source Web TechnologiesLatest Trends in Open Source Web Technologies
Latest Trends in Open Source Web Technologies
 
What it takes to be a Web Developer
What it takes to be a Web DeveloperWhat it takes to be a Web Developer
What it takes to be a Web Developer
 
before you leap
before you leapbefore you leap
before you leap
 
Sowing the Seeds
Sowing the SeedsSowing the Seeds
Sowing the Seeds
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

Ruby and Rails by Example (GeekCamp edition)

  • 1. Ruby and Rails by example
  • 2.
  • 3. Ruby is simple in appearance, but is very complex inside, just like our human body. - Yukihiro "matz" Matsumoto, creator of Ruby
  • 4. Example 0: Hash / Dictionary
  • 5. // 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");
  • 6. # Using Ruby openWith = { "txt" => "notepad.exe", "bmp" => "paint.exe", "dib" => "paint.exe", "rtf" => "wordpad.exe" }
  • 7. # Using Ruby 1.9 openWith = { txt: "notepad.exe", bmp: "paint.exe", dib: "paint.exe", rtf: "wordpad.exe" }
  • 8.
  • 9. DO MORE with LESS CODE
  • 10. // 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");
  • 13. Example 2: Create a Binary Tree
  • 14. 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) end end
  • 16. 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
  • 17. 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
  • 18. Example 3: Create a Person → Student → College Student class hierarchy
  • 19. class Person attr_accessor :name end class Student < Person attr_accessor :school end class CollegeStudent < Student attr_accessor :course end x = CollegeStudent.new x.name = "John Doe" x.school = "ABC University" x.course = "Computer Science"
  • 20. Example 4: Call a method in a "primitive"
  • 22. Example 5: Find the sum of the squares of all numbers under 10,000 divisible by 3 and/or 5
  • 23. x = 1 sum = 0 while x < 10000 do if x % 3 == 0 or x % 5 == 0 sum += x * x end end puts sum
  • 24. puts (1..10000). select { |x| x % 3 == 0 or x % 5 == 0}. map {|x| x * x }. reduce(:+)
  • 25. Example 6: Find all employees older than 30 and sort by last name
  • 26. oldies = employees.select { |e| e.age > 30 }. sort { |e1, e2| e1.last_name <=> e2.last_name }
  • 27. Example 7: Assign a method to a variable
  • 28. hello = Proc.new { |string| puts "Hello #{string}" } hello.call "Alice"
  • 29. Example 8: Add a "plus" method to all numbers
  • 30. class Numeric def plus(value) self.+(value) end end
  • 31. Example 9: Define different behavior for different instances
  • 32. alice = Person.new bob = Person.new alice.instance_eval do def hello puts "Hello" end end def bob.hello puts "Howdy!" end
  • 33. Example 10: Make Duck and Person swim
  • 34. module Swimmer def swim puts "This #{self.class} is swimming" end end class Duck include Swimmer end class Person include Swimmer end Duck.new.swim Student.new.swim
  • 35.
  • 36.
  • 37. Example 0: Make a Twitter Clone
  • 38. $ rails new twitclone $ cd twitclone $ rails generate scaffold tweet message:string $ rake db:migrate $ rails server
  • 39. $ rails new twitclone $ cd twitclone $ rails generate scaffold tweet message:string $ rake db:migrate $ rails server
  • 41.
  • 43. Dynamic Object Oriented Functional Metaprogramming
  • 44. +
  • 46. MVC CoC DRY TDD REST
  • 49. DO MORE with LESS CODE
  • 50. Rails Example: Demo a Twitter Clone
  • 51. https://github.com/bryanbibat/microblog31 Authentication – Devise Attachments – Paperclip Pagination – Kaminari Template Engine – Haml UI – Twitter Bootstrap
  • 52. Ruby Resources main site http://www.ruby-lang.org tutorials http://tryruby.org http://ruby.learncodethehardway.org/ http://mislav.uniqpath.com/poignant-guide/
  • 53. Rails Resources main site http://rubyonrails.org/ tutorials http://ruby.railstutorial.org/ http://railsforzombies.org/ Windows Installer http://railsinstaller.org/
  • 54. 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