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

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
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
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 

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