SlideShare a Scribd company logo
intro to ruby
                              brian hogan
               New Auburn Personal Computer Services LLC




twitter: bphogan
email: brianhogan at napcs.com
programming is fun.


twitter: bphogan
email: brianhogan at napcs.com
you just don’t know it
                     yet.

twitter: bphogan
email: brianhogan at napcs.com
I was a designer. I hated
              programming.

twitter: bphogan
email: brianhogan at napcs.com
my clients wanted
             interactive websites...

twitter: bphogan
email: brianhogan at napcs.com
and I started to hate
                     my life.


twitter: bphogan
email: brianhogan at napcs.com
I learned Ruby in 2005
               and fell in love...

twitter: bphogan
email: brianhogan at napcs.com
twitter: bphogan
email: brianhogan at napcs.com
So what can kinds of
           things can you do with
                    Ruby?

twitter: bphogan
email: brianhogan at napcs.com
Shoes.app do
    para "Item name"
    @name = edit_line

    button "Add to list" do
     @names.append do
      para @name.text
     end
     @name.text = ""
    end

    button("Clear the list") {@names.clear}

    @names = stack :width=>"100%", :height=>"90%"

   end




twitter: bphogan
email: brianhogan at napcs.com
require 'sinatra'
require 'pathname'

get "/" do
 dir = "./files/"
 @links = Dir[dir+"*"].map { |file|
   file_link(file)
 }.join
 erb :index
end

helpers do

 def file_link(file)
  filename = Pathname.new(file).basename
  "<li><a href='#{file}' target='_self'>#{filename}</a></li>"
 end

end

use_in_file_templates!

__END__

@@ index
<html>
 <head>
  <meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
  <style type="text/css" media="screen">@import "/stylesheets/iui.css";</style>
  <script type="application/x-javascript" src="/javascripts/iui.js"></script>
 </head>

 <body>
  <div class="toolbar">
  <h1 id="pageTitle"></h1>
  </div>
  <ul id="home" title="Your files, sir." selected="true">
    <%= @links %>
  </ul>
 </body>

</html>


twitter: bphogan
email: brianhogan at napcs.com
!the_border = 1px
     !base_color = #111
                                        #header {
     #header                              color: #333333;
       color = !base_color * 3            border-left: 1px;
       border-left= !the_border           border-right: 2px;
       border-right = !the_border * 2     color: red; }
       color: red                         #header a {
                                            font-weight: bold;
       a                                    text-decoration: none; }
           font-weight: bold
           text-decoration: none




twitter: bphogan
email: brianhogan at napcs.com
got a great idea and
         want to get it out there
                quickly?

twitter: bphogan
email: brianhogan at napcs.com
twitter: bphogan
email: brianhogan at napcs.com
got a big site that’s hard
               to maintain?

twitter: bphogan
email: brianhogan at napcs.com
twitter: bphogan
email: brianhogan at napcs.com
So, what is Ruby?
• Highly dynamic
• Very high level
• 100% object oriented
• 100% open-source
• Really easy to learn
History

    Smalltalk          C++         Ruby     Java     VB 6      C#
     (1983)           (1989)      (1993)   (1995)   (1996)   (2000)




twitter: bphogan
email: brianhogan at napcs.com
twitter: bphogan
email: brianhogan at napcs.com
Ruby follows the
                    Principle of Least
                        Surprise.

twitter: bphogan
email: brianhogan at napcs.com
consistent API
"Brian".length
["red", "green", "blue"].length
[:first_name => "Brian", :last_name => "Hogan"].length
User.find_all_by_last_name("Hogan").length
and a simple syntax
age = 42
first_name = "Homer"
start_date = Date.new 1980, 06, 05
annual_salary = 100000.00
Basic Ruby


twitter: bphogan
email: brianhogan at napcs.com
Standard operators


 5 + 5
 10 * 10
 "Hello" + "World"
 25 / 5
Arrays


colors = ["Red", "Green", "Blue"]
Hashes (Dictionaries)


attributes = {:age => 25,
              :first_name => "Homer",
              :last_name => "Simpson"}
=>
:foo


twitter: bphogan
email: brianhogan at napcs.com
Simple control logic
if on_probation(start_date)
  puts "Yes"
else
  puts "no"
end
Unless
if !current_user.admin?
  redirect_to "/login"
end




unless current_user.admin?
  redirect_to "/login"
end
Conditionals as statement suffixes

redirect_to "/login" unless current_user.admin?
Methods (functions) are simple too.


 # if start date + 6 months is > today
 def on_probation?(start_date)
   (start_date >> 6) > Date.today
 end
Classes are easy too.
class Person
  @started_on = Date.today
  @name = ""

  def started_on=(date)
    @started_on = date
  end

  def started_on
    @started_on
  end

end
Class instance variables are private
class Person
  @started_on = Date.today
                               Expose them through
  @name = ""
                               accessor methods that
  def started_on=(date)
    @started_on = date          resemble C# and VB
  end
                                 Property members.
  def started_on
    @started_on
  end                            person = Person.new
                                 person.age = 32
  def name=(name)                person.name = "Brian"
    @name = name
  end
                                 person.age
                                 => 32
  def name                       person.name
    @name
                                 => "Brian"
  end
end
Let Ruby write code for you!
class Person
  @started_on = Date.today
  @name = ""

  def started_on=(date)
                                  class Person
    @started_on = date
  end                               attr_accessor :name
                                    attr_accessor :started_on
  def started_on
    @started_on                   end
  end

  def name=(name)
    @name = name
  end

  def name
    @name
  end
end
Ruby is a loaded gun.


twitter: bphogan
email: brianhogan at napcs.com
So, write good tests.


twitter: bphogan
email: brianhogan at napcs.com
TATFT


twitter: bphogan
email: brianhogan at napcs.com
def test_user_hired_today_should_be_on_probation
  person = Person.new
  person.hired_on = Date.today
  assert person.on_probation?
end


test_user_hired_last_year_should_not_be_on_probation
  person = Person.new
  person.hired_on = 1.year.ago
  assert !person.on_probation?
end
Implement the method
class Person
  attr_accessor :name, :start_date

  def on_probation?
    (start_date >> 6) > Date.today
  end
end
And the tests pass.


twitter: bphogan
email: brianhogan at napcs.com
Testing first helps you
         think about your design
           AND your features.

twitter: bphogan
email: brianhogan at napcs.com
Every professional Ruby
         developer writes tests
          first for production
                 code.
twitter: bphogan
email: brianhogan at napcs.com
Ten Simple Rules
 For Programming in
My Favorite Language.
1. Everything is an
                         object


twitter: bphogan
email: brianhogan at napcs.com
25.class     Fixnum



"brian".class   String



[1,2,3].class   Array
2. Everything evaluates
            to true except nil or
                    false

twitter: bphogan
email: brianhogan at napcs.com
x = 0
              0
puts x if x



x = -1
              -1
puts x if x



x = false
              nil
puts x if x



x = nil
              nil
puts x if x
3. Variables are
          dynamically typed but
         DATA is strongly typed!

twitter: bphogan
email: brianhogan at napcs.com
age = 32
name = "Brian"    TypeError: can't convert Fixnum into String
name + age




age = 32
name = "Brian"                    “Brian32”
name + age.to_s
4. Every method
                  returns the last
                evaluation implicitly

twitter: bphogan
email: brianhogan at napcs.com
def limit_reached?
  self.projects.length > 0
end


def welcome_message
  if current_user.anonymous?
    "You need to log in."
  else
    "Welcome!"
  end
end
5. Every expression
            evaluates to an object


twitter: bphogan
email: brianhogan at napcs.com
result = if current_user.anonymous?
           "You need to log in."
         else
           "Welcome!"
         end
It is very easy to accidentally return false!
       def before_save
         if self.status == "closed"
           self.closed_on = Date.today
         end
       end
6. Classes are objects


twitter: bphogan
email: brianhogan at napcs.com
WRONG!
person = new Person




    RIGHT!
person = Person.new
7. You need to use and
             understand blocks.

twitter: bphogan
email: brianhogan at napcs.com
Blocks can iterate


roles.each do |role|
  puts "<li>" + role.name + "<li>"
end
They can also encapsulate code.



  ActiveRecord::Schema.define do
    create_table :pages do |t|
      t.string :name
      t.text :body
      t.timestamps
    end
  end
Every method can take a block!




      5.times do
        puts "Hello!"
      end
8. Favor modules over
                  inheritance


twitter: bphogan
email: brianhogan at napcs.com
module SharedValidations
         def self.included(base)
           base.validates_presence_of :name
           base.validates_uniqueness_of :name
         end
       end




class Project                 class Task
  include SharedValidations     include SharedValidations
end                           end
Do not use type,
                     use behaviors.


twitter: bphogan
email: brianhogan at napcs.com
twitter: bphogan
email: brianhogan at napcs.com
module Doctor            module Ninja
  def treat_patient        def attack
    puts "All better!"       puts "You’re dead!"
  end                      end
end                      end


module Musician
  def play_guitar
    puts "meedily-meedily-meedily-meeeeeeeeee!"
  end
end
person = Person.new
person.extend Ninja      "You're dead!"
person.attack


person.extend Doctor
                         "All better!"
person.treat_patient



person.extend Musician   "meedily-meedily-
person.play_guitar       meedily-meeeeeeeeee!"
9. Embrace Reflection


twitter: bphogan
email: brianhogan at napcs.com
person.respond_to?(:name)   true




person.respond_to?(:age)    false




person.send(:name)          “Brian”
10. Write code that
                    writes code.

twitter: bphogan
email: brianhogan at napcs.com
class User
  ROLES = ["admin", "superadmin", "user", "moderator"]
  ROLES.each do |role|
    class_eval <<-EOF
      def #{role}?
        self.roles.include?("#{role}")
      end
    EOF
  end
end




                user = User.new
                user.admin?
                user.moderator?
haml and sass


twitter: bphogan
email: brianhogan at napcs.com
HAML
!!!
#wrapper.container_12
  #header.grid_12
    %h1 The awesome site
  %ul#navbar.grid_12
    %li
      %a{:href => "index.html"} Home
    %li
      %a{:href => "products"} Products
    %li
      %a{:href => "services"} Services
  #middle.grid_12
    %h2 Welcome

 #footer.grid_12
   %p Copyright 2009 SomeCompany
HTML
<div class='container_12' id='wrapper'>
   <div class='grid_12' id='header'>
     <h1>The awesome site</h1>
   </div>
   <ul class='grid_12' id='navbar'>
     <li>
        <a href='index.html'>Home</a>
     </li>
     <li>
        <a href='products'>Products</a>
     </li>
     <li>
        <a href='services'>Services</a>
     </li>
   </ul>
   <div class='grid_12' id='middle'>
     <h2>Welcome</h2>
   </div>
   <div class='grid_12' id='footer'>
     <p>Copyright 2009 SomeCompany</p>
   </div>
 </div>
SASS

!the_border = 1px
!base_color = #111

#header
  color = !base_color * 3
  border-left= !the_border
  border-right = !the_border * 2
  color: red

 a
     font-weight: bold
     text-decoration: none
CSS

#header {
  color: #333333;
  border-left: 1px;
  border-right: 2px;
  color: red; }

 #header a {
   font-weight: bold;
   text-decoration: none; }
StaticMatic demo


twitter: bphogan
email: brianhogan at napcs.com
sinatra


twitter: bphogan
email: brianhogan at napcs.com
Hello Sinatra!


require 'rubygems'
require 'sinatra'

get "/" do
  "Hello Sinatra!"
end
Sinatra demo


twitter: bphogan
email: brianhogan at napcs.com
cucumber


twitter: bphogan
email: brianhogan at napcs.com
Feature: creating a new page in the wiki
As an average anonymous user
I want to create a page about Ruby
So that I can tell everyone how awesome it is.

Scenario: Creating a new page and editing its content
Given I go to "/ruby"
 Then I should see "Edit this page"
 When I click "Edit this page"
 And I fill in "body" with "Ruby is the best programming language in the whole world!"
 And I press "Save"
 Then I should see "Ruby is the best programming language in the whole world!"




twitter: bphogan
email: brianhogan at napcs.com
Testing the Wiki with
          Webrat and Cucumber


twitter: bphogan
email: brianhogan at napcs.com
Ruby will make you
                   productive.


twitter: bphogan
email: brianhogan at napcs.com
And happy.


twitter: bphogan
email: brianhogan at napcs.com
Resources:
 Try Ruby in your browser! http://tryruby.sophrinix.com/
         Try SASS online: http://sass-lang.com/try.html
        Try HAML online: http://haml-lang.com/try.html
                http://staticmatic.rubyforge.org/
              Sinatra: http://www.sinatrarb.com/
     Sinatra Wiki source: http://github.com/napcs/sinatriki
                  Cucumber: http://cukes.info/
                   WATIR: http://watir.com/

twitter: bphogan
email: brianhogan at napcs.com
Questions?
             Twitter: bphogan
         brianhogan at napcs.com

twitter: bphogan
email: brianhogan at napcs.com

More Related Content

What's hot

Facebook Black book 3 - make money online everyday
Facebook Black book 3 - make money online everydayFacebook Black book 3 - make money online everyday
Facebook Black book 3 - make money online everyday
Edward806784
 
WRA 210 April 14th PowerPoint
WRA 210 April 14th PowerPointWRA 210 April 14th PowerPoint
WRA 210 April 14th PowerPoint
Miami University
 
WordPress Development Confoo 2010
WordPress Development Confoo 2010WordPress Development Confoo 2010
WordPress Development Confoo 2010
Brendan Sera-Shriar
 
Hide email address in sourc...
Hide email address in sourc...Hide email address in sourc...
Hide email address in sourc...
chaitanya535
 
Writing Clean, Standards Compliant, Testable Code for WordPress
Writing Clean, Standards Compliant, Testable Code for WordPressWriting Clean, Standards Compliant, Testable Code for WordPress
Writing Clean, Standards Compliant, Testable Code for WordPress
Shawn Hooper
 
Advanced Json
Advanced JsonAdvanced Json
Advanced Json
guestfd7d7c
 
2600 Thailand #50 From 0day to CVE
2600 Thailand #50 From 0day to CVE2600 Thailand #50 From 0day to CVE
2600 Thailand #50 From 0day to CVE
Pongtorn Angsuchotmetee
 
I put on my mink and wizard behat
I put on my mink and wizard behatI put on my mink and wizard behat
I put on my mink and wizard behat
xsist10
 
Lecture 3 - Comm Lab: Web @ ITP
Lecture 3 - Comm Lab: Web @ ITP Lecture 3 - Comm Lab: Web @ ITP
Lecture 3 - Comm Lab: Web @ ITP
yucefmerhi
 

What's hot (9)

Facebook Black book 3 - make money online everyday
Facebook Black book 3 - make money online everydayFacebook Black book 3 - make money online everyday
Facebook Black book 3 - make money online everyday
 
WRA 210 April 14th PowerPoint
WRA 210 April 14th PowerPointWRA 210 April 14th PowerPoint
WRA 210 April 14th PowerPoint
 
WordPress Development Confoo 2010
WordPress Development Confoo 2010WordPress Development Confoo 2010
WordPress Development Confoo 2010
 
Hide email address in sourc...
Hide email address in sourc...Hide email address in sourc...
Hide email address in sourc...
 
Writing Clean, Standards Compliant, Testable Code for WordPress
Writing Clean, Standards Compliant, Testable Code for WordPressWriting Clean, Standards Compliant, Testable Code for WordPress
Writing Clean, Standards Compliant, Testable Code for WordPress
 
Advanced Json
Advanced JsonAdvanced Json
Advanced Json
 
2600 Thailand #50 From 0day to CVE
2600 Thailand #50 From 0day to CVE2600 Thailand #50 From 0day to CVE
2600 Thailand #50 From 0day to CVE
 
I put on my mink and wizard behat
I put on my mink and wizard behatI put on my mink and wizard behat
I put on my mink and wizard behat
 
Lecture 3 - Comm Lab: Web @ ITP
Lecture 3 - Comm Lab: Web @ ITP Lecture 3 - Comm Lab: Web @ ITP
Lecture 3 - Comm Lab: Web @ ITP
 

Similar to Intro to Ruby

Intro To Advanced Ruby
Intro To Advanced RubyIntro To Advanced Ruby
Intro To Advanced Ruby
Brian Hogan
 
Building A Gem From Scratch
Building A Gem From ScratchBuilding A Gem From Scratch
Building A Gem From Scratch
Brian Hogan
 
Rails OO views
Rails OO viewsRails OO views
Rails OO views
Elia Schito
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
Luigi De Russis
 
Idea2app
Idea2appIdea2app
Idea2app
Flumes
 
How to become hacker
How to become hackerHow to become hacker
How to become hacker
Raman Sanoria
 
ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1
UCLA Association of Computing Machinery
 
Write your Ruby in Style
Write your Ruby in StyleWrite your Ruby in Style
Write your Ruby in Style
Bhavin Javia
 
GDG DART Event at Karachi
GDG DART Event at KarachiGDG DART Event at Karachi
GDG DART Event at Karachi
Imam Raza
 
Python slide
Python slidePython slide
Rapid web development, the right way.
Rapid web development, the right way.Rapid web development, the right way.
Rapid web development, the right way.
nubela
 
Python: an introduction for PHP webdevelopers
Python: an introduction for PHP webdevelopersPython: an introduction for PHP webdevelopers
Python: an introduction for PHP webdevelopers
Glenn De Backer
 
Python introduction 2
Python introduction 2Python introduction 2
Python introduction 2
Ahmad Hussein
 
Prototyping w/HTML5 and CSS3
Prototyping w/HTML5 and CSS3Prototyping w/HTML5 and CSS3
Prototyping w/HTML5 and CSS3
Todd Zaki Warfel
 
Nullcon Hack IM 2011 walk through
Nullcon Hack IM 2011 walk throughNullcon Hack IM 2011 walk through
Nullcon Hack IM 2011 walk through
Anant Shrivastava
 
Python in 30 minutes!
Python in 30 minutes!Python in 30 minutes!
Python in 30 minutes!
Fariz Darari
 
Learn Python 3 for absolute beginners
Learn Python 3 for absolute beginnersLearn Python 3 for absolute beginners
Learn Python 3 for absolute beginners
KingsleyAmankwa
 
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
Bozhidar Batsov
 
Testing Has Many Purposes
Testing Has Many PurposesTesting Has Many Purposes
Testing Has Many Purposes
Alex Sharp
 
CYB 130 RANK Achievement Education--cyb130rank.com
CYB 130 RANK Achievement Education--cyb130rank.comCYB 130 RANK Achievement Education--cyb130rank.com
CYB 130 RANK Achievement Education--cyb130rank.com
agathachristie198
 

Similar to Intro to Ruby (20)

Intro To Advanced Ruby
Intro To Advanced RubyIntro To Advanced Ruby
Intro To Advanced Ruby
 
Building A Gem From Scratch
Building A Gem From ScratchBuilding A Gem From Scratch
Building A Gem From Scratch
 
Rails OO views
Rails OO viewsRails OO views
Rails OO views
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
 
Idea2app
Idea2appIdea2app
Idea2app
 
How to become hacker
How to become hackerHow to become hacker
How to become hacker
 
ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1
 
Write your Ruby in Style
Write your Ruby in StyleWrite your Ruby in Style
Write your Ruby in Style
 
GDG DART Event at Karachi
GDG DART Event at KarachiGDG DART Event at Karachi
GDG DART Event at Karachi
 
Python slide
Python slidePython slide
Python slide
 
Rapid web development, the right way.
Rapid web development, the right way.Rapid web development, the right way.
Rapid web development, the right way.
 
Python: an introduction for PHP webdevelopers
Python: an introduction for PHP webdevelopersPython: an introduction for PHP webdevelopers
Python: an introduction for PHP webdevelopers
 
Python introduction 2
Python introduction 2Python introduction 2
Python introduction 2
 
Prototyping w/HTML5 and CSS3
Prototyping w/HTML5 and CSS3Prototyping w/HTML5 and CSS3
Prototyping w/HTML5 and CSS3
 
Nullcon Hack IM 2011 walk through
Nullcon Hack IM 2011 walk throughNullcon Hack IM 2011 walk through
Nullcon Hack IM 2011 walk through
 
Python in 30 minutes!
Python in 30 minutes!Python in 30 minutes!
Python in 30 minutes!
 
Learn Python 3 for absolute beginners
Learn Python 3 for absolute beginnersLearn Python 3 for absolute beginners
Learn Python 3 for absolute beginners
 
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
 
Testing Has Many Purposes
Testing Has Many PurposesTesting Has Many Purposes
Testing Has Many Purposes
 
CYB 130 RANK Achievement Education--cyb130rank.com
CYB 130 RANK Achievement Education--cyb130rank.comCYB 130 RANK Achievement Education--cyb130rank.com
CYB 130 RANK Achievement Education--cyb130rank.com
 

More from Brian Hogan

Creating and Deploying Static Sites with Hugo
Creating and Deploying Static Sites with HugoCreating and Deploying Static Sites with Hugo
Creating and Deploying Static Sites with Hugo
Brian Hogan
 
Automating the Cloud with Terraform, and Ansible
Automating the Cloud with Terraform, and AnsibleAutomating the Cloud with Terraform, and Ansible
Automating the Cloud with Terraform, and Ansible
Brian Hogan
 
Create Development and Production Environments with Vagrant
Create Development and Production Environments with VagrantCreate Development and Production Environments with Vagrant
Create Development and Production Environments with Vagrant
Brian Hogan
 
Docker
DockerDocker
Docker
Brian Hogan
 
Getting Started Contributing To Open Source
Getting Started Contributing To Open SourceGetting Started Contributing To Open Source
Getting Started Contributing To Open Source
Brian Hogan
 
Rethink Frontend Development With Elm
Rethink Frontend Development With ElmRethink Frontend Development With Elm
Rethink Frontend Development With Elm
Brian Hogan
 
Testing Client-side Code with Jasmine and CoffeeScript
Testing Client-side Code with Jasmine and CoffeeScriptTesting Client-side Code with Jasmine and CoffeeScript
Testing Client-side Code with Jasmine and CoffeeScript
Brian Hogan
 
FUD-Free Accessibility for Web Developers - Also, Cake.
FUD-Free Accessibility for Web Developers - Also, Cake.FUD-Free Accessibility for Web Developers - Also, Cake.
FUD-Free Accessibility for Web Developers - Also, Cake.
Brian Hogan
 
Responsive Web Design
Responsive Web DesignResponsive Web Design
Responsive Web Design
Brian Hogan
 
Web Development with CoffeeScript and Sass
Web Development with CoffeeScript and SassWeb Development with CoffeeScript and Sass
Web Development with CoffeeScript and Sass
Brian Hogan
 
HTML5 and CSS3 Today
HTML5 and CSS3 TodayHTML5 and CSS3 Today
HTML5 and CSS3 Today
Brian Hogan
 
Web Development With Ruby - From Simple To Complex
Web Development With Ruby - From Simple To ComplexWeb Development With Ruby - From Simple To Complex
Web Development With Ruby - From Simple To Complex
Brian Hogan
 
Stop Reinventing The Wheel - The Ruby Standard Library
Stop Reinventing The Wheel - The Ruby Standard LibraryStop Reinventing The Wheel - The Ruby Standard Library
Stop Reinventing The Wheel - The Ruby Standard Library
Brian Hogan
 
Make GUI Apps with Shoes
Make GUI Apps with ShoesMake GUI Apps with Shoes
Make GUI Apps with Shoes
Brian Hogan
 
The Why Of Ruby
The Why Of RubyThe Why Of Ruby
The Why Of Ruby
Brian Hogan
 
Learning To Walk In Shoes
Learning To Walk In ShoesLearning To Walk In Shoes
Learning To Walk In Shoes
Brian Hogan
 
Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009
Brian Hogan
 

More from Brian Hogan (17)

Creating and Deploying Static Sites with Hugo
Creating and Deploying Static Sites with HugoCreating and Deploying Static Sites with Hugo
Creating and Deploying Static Sites with Hugo
 
Automating the Cloud with Terraform, and Ansible
Automating the Cloud with Terraform, and AnsibleAutomating the Cloud with Terraform, and Ansible
Automating the Cloud with Terraform, and Ansible
 
Create Development and Production Environments with Vagrant
Create Development and Production Environments with VagrantCreate Development and Production Environments with Vagrant
Create Development and Production Environments with Vagrant
 
Docker
DockerDocker
Docker
 
Getting Started Contributing To Open Source
Getting Started Contributing To Open SourceGetting Started Contributing To Open Source
Getting Started Contributing To Open Source
 
Rethink Frontend Development With Elm
Rethink Frontend Development With ElmRethink Frontend Development With Elm
Rethink Frontend Development With Elm
 
Testing Client-side Code with Jasmine and CoffeeScript
Testing Client-side Code with Jasmine and CoffeeScriptTesting Client-side Code with Jasmine and CoffeeScript
Testing Client-side Code with Jasmine and CoffeeScript
 
FUD-Free Accessibility for Web Developers - Also, Cake.
FUD-Free Accessibility for Web Developers - Also, Cake.FUD-Free Accessibility for Web Developers - Also, Cake.
FUD-Free Accessibility for Web Developers - Also, Cake.
 
Responsive Web Design
Responsive Web DesignResponsive Web Design
Responsive Web Design
 
Web Development with CoffeeScript and Sass
Web Development with CoffeeScript and SassWeb Development with CoffeeScript and Sass
Web Development with CoffeeScript and Sass
 
HTML5 and CSS3 Today
HTML5 and CSS3 TodayHTML5 and CSS3 Today
HTML5 and CSS3 Today
 
Web Development With Ruby - From Simple To Complex
Web Development With Ruby - From Simple To ComplexWeb Development With Ruby - From Simple To Complex
Web Development With Ruby - From Simple To Complex
 
Stop Reinventing The Wheel - The Ruby Standard Library
Stop Reinventing The Wheel - The Ruby Standard LibraryStop Reinventing The Wheel - The Ruby Standard Library
Stop Reinventing The Wheel - The Ruby Standard Library
 
Make GUI Apps with Shoes
Make GUI Apps with ShoesMake GUI Apps with Shoes
Make GUI Apps with Shoes
 
The Why Of Ruby
The Why Of RubyThe Why Of Ruby
The Why Of Ruby
 
Learning To Walk In Shoes
Learning To Walk In ShoesLearning To Walk In Shoes
Learning To Walk In Shoes
 
Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009
 

Recently uploaded

Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
UiPathCommunity
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
UiPathCommunity
 
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance PanelsNorthern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Neo4j
 
Introducing BoxLang : A new JVM language for productivity and modularity!
Introducing BoxLang : A new JVM language for productivity and modularity!Introducing BoxLang : A new JVM language for productivity and modularity!
Introducing BoxLang : A new JVM language for productivity and modularity!
Ortus Solutions, Corp
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
Enterprise Knowledge
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
Fwdays
 
Getting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
Getting the Most Out of ScyllaDB Monitoring: ShareChat's TipsGetting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
Getting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
ScyllaDB
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
Fwdays
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 
AWS Certified Solutions Architect Associate (SAA-C03)
AWS Certified Solutions Architect Associate (SAA-C03)AWS Certified Solutions Architect Associate (SAA-C03)
AWS Certified Solutions Architect Associate (SAA-C03)
HarpalGohil4
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
Jason Yip
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
Mydbops
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
zjhamm304
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
Fwdays
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
christinelarrosa
 

Recently uploaded (20)

Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
 
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance PanelsNorthern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
 
Introducing BoxLang : A new JVM language for productivity and modularity!
Introducing BoxLang : A new JVM language for productivity and modularity!Introducing BoxLang : A new JVM language for productivity and modularity!
Introducing BoxLang : A new JVM language for productivity and modularity!
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
 
Getting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
Getting the Most Out of ScyllaDB Monitoring: ShareChat's TipsGetting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
Getting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 
AWS Certified Solutions Architect Associate (SAA-C03)
AWS Certified Solutions Architect Associate (SAA-C03)AWS Certified Solutions Architect Associate (SAA-C03)
AWS Certified Solutions Architect Associate (SAA-C03)
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
 

Intro to Ruby

  • 1. intro to ruby brian hogan New Auburn Personal Computer Services LLC twitter: bphogan email: brianhogan at napcs.com
  • 2. programming is fun. twitter: bphogan email: brianhogan at napcs.com
  • 3. you just don’t know it yet. twitter: bphogan email: brianhogan at napcs.com
  • 4. I was a designer. I hated programming. twitter: bphogan email: brianhogan at napcs.com
  • 5. my clients wanted interactive websites... twitter: bphogan email: brianhogan at napcs.com
  • 6. and I started to hate my life. twitter: bphogan email: brianhogan at napcs.com
  • 7. I learned Ruby in 2005 and fell in love... twitter: bphogan email: brianhogan at napcs.com
  • 9. So what can kinds of things can you do with Ruby? twitter: bphogan email: brianhogan at napcs.com
  • 10. Shoes.app do para "Item name" @name = edit_line button "Add to list" do @names.append do para @name.text end @name.text = "" end button("Clear the list") {@names.clear} @names = stack :width=>"100%", :height=>"90%" end twitter: bphogan email: brianhogan at napcs.com
  • 11. require 'sinatra' require 'pathname' get "/" do dir = "./files/" @links = Dir[dir+"*"].map { |file| file_link(file) }.join erb :index end helpers do def file_link(file) filename = Pathname.new(file).basename "<li><a href='#{file}' target='_self'>#{filename}</a></li>" end end use_in_file_templates! __END__ @@ index <html> <head> <meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/> <style type="text/css" media="screen">@import "/stylesheets/iui.css";</style> <script type="application/x-javascript" src="/javascripts/iui.js"></script> </head> <body> <div class="toolbar"> <h1 id="pageTitle"></h1> </div> <ul id="home" title="Your files, sir." selected="true"> <%= @links %> </ul> </body> </html> twitter: bphogan email: brianhogan at napcs.com
  • 12. !the_border = 1px !base_color = #111 #header { #header color: #333333; color = !base_color * 3 border-left: 1px; border-left= !the_border border-right: 2px; border-right = !the_border * 2 color: red; } color: red #header a { font-weight: bold; a text-decoration: none; } font-weight: bold text-decoration: none twitter: bphogan email: brianhogan at napcs.com
  • 13. got a great idea and want to get it out there quickly? twitter: bphogan email: brianhogan at napcs.com
  • 15. got a big site that’s hard to maintain? twitter: bphogan email: brianhogan at napcs.com
  • 17. So, what is Ruby? • Highly dynamic • Very high level • 100% object oriented • 100% open-source • Really easy to learn
  • 18. History Smalltalk C++ Ruby Java VB 6 C# (1983) (1989) (1993) (1995) (1996) (2000) twitter: bphogan email: brianhogan at napcs.com
  • 20. Ruby follows the Principle of Least Surprise. twitter: bphogan email: brianhogan at napcs.com
  • 21. consistent API "Brian".length ["red", "green", "blue"].length [:first_name => "Brian", :last_name => "Hogan"].length User.find_all_by_last_name("Hogan").length
  • 22. and a simple syntax age = 42 first_name = "Homer" start_date = Date.new 1980, 06, 05 annual_salary = 100000.00
  • 23. Basic Ruby twitter: bphogan email: brianhogan at napcs.com
  • 24. Standard operators 5 + 5 10 * 10 "Hello" + "World" 25 / 5
  • 25. Arrays colors = ["Red", "Green", "Blue"]
  • 26. Hashes (Dictionaries) attributes = {:age => 25, :first_name => "Homer", :last_name => "Simpson"}
  • 27. =>
  • 29. Simple control logic if on_probation(start_date) puts "Yes" else puts "no" end
  • 30. Unless if !current_user.admin? redirect_to "/login" end unless current_user.admin? redirect_to "/login" end
  • 31. Conditionals as statement suffixes redirect_to "/login" unless current_user.admin?
  • 32. Methods (functions) are simple too. # if start date + 6 months is > today def on_probation?(start_date) (start_date >> 6) > Date.today end
  • 33. Classes are easy too. class Person @started_on = Date.today @name = "" def started_on=(date) @started_on = date end def started_on @started_on end end
  • 34. Class instance variables are private class Person @started_on = Date.today Expose them through @name = "" accessor methods that def started_on=(date) @started_on = date resemble C# and VB end Property members. def started_on @started_on end person = Person.new person.age = 32 def name=(name) person.name = "Brian" @name = name end person.age => 32 def name person.name @name => "Brian" end end
  • 35. Let Ruby write code for you! class Person @started_on = Date.today @name = "" def started_on=(date) class Person @started_on = date end attr_accessor :name attr_accessor :started_on def started_on @started_on end end def name=(name) @name = name end def name @name end end
  • 36. Ruby is a loaded gun. twitter: bphogan email: brianhogan at napcs.com
  • 37. So, write good tests. twitter: bphogan email: brianhogan at napcs.com
  • 39. def test_user_hired_today_should_be_on_probation person = Person.new person.hired_on = Date.today assert person.on_probation? end test_user_hired_last_year_should_not_be_on_probation person = Person.new person.hired_on = 1.year.ago assert !person.on_probation? end
  • 40. Implement the method class Person attr_accessor :name, :start_date def on_probation? (start_date >> 6) > Date.today end end
  • 41. And the tests pass. twitter: bphogan email: brianhogan at napcs.com
  • 42. Testing first helps you think about your design AND your features. twitter: bphogan email: brianhogan at napcs.com
  • 43. Every professional Ruby developer writes tests first for production code. twitter: bphogan email: brianhogan at napcs.com
  • 44. Ten Simple Rules For Programming in My Favorite Language.
  • 45. 1. Everything is an object twitter: bphogan email: brianhogan at napcs.com
  • 46. 25.class Fixnum "brian".class String [1,2,3].class Array
  • 47. 2. Everything evaluates to true except nil or false twitter: bphogan email: brianhogan at napcs.com
  • 48. x = 0 0 puts x if x x = -1 -1 puts x if x x = false nil puts x if x x = nil nil puts x if x
  • 49. 3. Variables are dynamically typed but DATA is strongly typed! twitter: bphogan email: brianhogan at napcs.com
  • 50. age = 32 name = "Brian" TypeError: can't convert Fixnum into String name + age age = 32 name = "Brian" “Brian32” name + age.to_s
  • 51. 4. Every method returns the last evaluation implicitly twitter: bphogan email: brianhogan at napcs.com
  • 52. def limit_reached? self.projects.length > 0 end def welcome_message if current_user.anonymous? "You need to log in." else "Welcome!" end end
  • 53. 5. Every expression evaluates to an object twitter: bphogan email: brianhogan at napcs.com
  • 54. result = if current_user.anonymous? "You need to log in." else "Welcome!" end
  • 55. It is very easy to accidentally return false! def before_save if self.status == "closed" self.closed_on = Date.today end end
  • 56. 6. Classes are objects twitter: bphogan email: brianhogan at napcs.com
  • 57. WRONG! person = new Person RIGHT! person = Person.new
  • 58. 7. You need to use and understand blocks. twitter: bphogan email: brianhogan at napcs.com
  • 59. Blocks can iterate roles.each do |role| puts "<li>" + role.name + "<li>" end
  • 60. They can also encapsulate code. ActiveRecord::Schema.define do create_table :pages do |t| t.string :name t.text :body t.timestamps end end
  • 61. Every method can take a block! 5.times do puts "Hello!" end
  • 62. 8. Favor modules over inheritance twitter: bphogan email: brianhogan at napcs.com
  • 63. module SharedValidations def self.included(base) base.validates_presence_of :name base.validates_uniqueness_of :name end end class Project class Task include SharedValidations include SharedValidations end end
  • 64. Do not use type, use behaviors. twitter: bphogan email: brianhogan at napcs.com
  • 66. module Doctor module Ninja def treat_patient def attack puts "All better!" puts "You’re dead!" end end end end module Musician def play_guitar puts "meedily-meedily-meedily-meeeeeeeeee!" end end
  • 67. person = Person.new person.extend Ninja "You're dead!" person.attack person.extend Doctor "All better!" person.treat_patient person.extend Musician "meedily-meedily- person.play_guitar meedily-meeeeeeeeee!"
  • 68. 9. Embrace Reflection twitter: bphogan email: brianhogan at napcs.com
  • 69. person.respond_to?(:name) true person.respond_to?(:age) false person.send(:name) “Brian”
  • 70. 10. Write code that writes code. twitter: bphogan email: brianhogan at napcs.com
  • 71. class User ROLES = ["admin", "superadmin", "user", "moderator"] ROLES.each do |role| class_eval <<-EOF def #{role}? self.roles.include?("#{role}") end EOF end end user = User.new user.admin? user.moderator?
  • 72. haml and sass twitter: bphogan email: brianhogan at napcs.com
  • 73. HAML !!! #wrapper.container_12 #header.grid_12 %h1 The awesome site %ul#navbar.grid_12 %li %a{:href => "index.html"} Home %li %a{:href => "products"} Products %li %a{:href => "services"} Services #middle.grid_12 %h2 Welcome #footer.grid_12 %p Copyright 2009 SomeCompany
  • 74. HTML <div class='container_12' id='wrapper'> <div class='grid_12' id='header'> <h1>The awesome site</h1> </div> <ul class='grid_12' id='navbar'> <li> <a href='index.html'>Home</a> </li> <li> <a href='products'>Products</a> </li> <li> <a href='services'>Services</a> </li> </ul> <div class='grid_12' id='middle'> <h2>Welcome</h2> </div> <div class='grid_12' id='footer'> <p>Copyright 2009 SomeCompany</p> </div> </div>
  • 75. SASS !the_border = 1px !base_color = #111 #header color = !base_color * 3 border-left= !the_border border-right = !the_border * 2 color: red a font-weight: bold text-decoration: none
  • 76. CSS #header { color: #333333; border-left: 1px; border-right: 2px; color: red; } #header a { font-weight: bold; text-decoration: none; }
  • 77. StaticMatic demo twitter: bphogan email: brianhogan at napcs.com
  • 79. Hello Sinatra! require 'rubygems' require 'sinatra' get "/" do "Hello Sinatra!" end
  • 80.
  • 81. Sinatra demo twitter: bphogan email: brianhogan at napcs.com
  • 83. Feature: creating a new page in the wiki As an average anonymous user I want to create a page about Ruby So that I can tell everyone how awesome it is. Scenario: Creating a new page and editing its content Given I go to "/ruby" Then I should see "Edit this page" When I click "Edit this page" And I fill in "body" with "Ruby is the best programming language in the whole world!" And I press "Save" Then I should see "Ruby is the best programming language in the whole world!" twitter: bphogan email: brianhogan at napcs.com
  • 84. Testing the Wiki with Webrat and Cucumber twitter: bphogan email: brianhogan at napcs.com
  • 85. Ruby will make you productive. twitter: bphogan email: brianhogan at napcs.com
  • 86. And happy. twitter: bphogan email: brianhogan at napcs.com
  • 87. Resources: Try Ruby in your browser! http://tryruby.sophrinix.com/ Try SASS online: http://sass-lang.com/try.html Try HAML online: http://haml-lang.com/try.html http://staticmatic.rubyforge.org/ Sinatra: http://www.sinatrarb.com/ Sinatra Wiki source: http://github.com/napcs/sinatriki Cucumber: http://cukes.info/ WATIR: http://watir.com/ twitter: bphogan email: brianhogan at napcs.com
  • 88. Questions? Twitter: bphogan brianhogan at napcs.com twitter: bphogan email: brianhogan at napcs.com

Editor's Notes

  1. Hi everyone. I&amp;#x2019;m Brian. I do Ruby and Rails training and consulting.
  2. Maybe you do... but it can be even more fun.
  3. I hated programming. I did some when I was a kid, but it wasn&amp;#x2019;t what I wanted to do. I liked the web. And I started building sites in 1995 for small businesses.
  4. so I started learning to program in ASP and eventually PHP. Even did some Java and some Oracle DBA stuff in there.
  5. I was getting burned out, spending hours fighting with the languages while writing the same kind of applications over again.
  6. A consultant who was working with me on a Java project introduced me to Rails and now, four years later,
  7. I get to work on fun projects, work with amazing people, I&amp;#x2019;m excited about what I do, and I even got to write some books.
  8. I want to get you excited about this language. I want to you to ask me any questions you have, and I want you to run home and start coding! So the best way to do that is to show you what you can do.
  9. We can make a desktop application that works on Windows, Mac, and Linux using Shoes.
  10. We can make a very simple iPhone-enabled website with Sinatra. This one serves files to you in around 50 lines of code.
  11. We can use Sass to generate stylesheets for our applications. We can use variables for our colors and widths!
  12. Use Rails. Rails is a great framework for building web applications. And despite what you&amp;#x2019;ve heard, it scales exceptionally well, as long as you know how to scale a web application and you&amp;#x2019;ve written good code.
  13. Use Rails to kickstart a CMS.
  14. Highly dynamic, high level, 100% object oriented, 100% open source, and really easy to learn.
  15. Ruby was created by Yukihiro Matsumoto (Matz) in 1993. It&amp;#x2019;s built on C, and has many implementations, including JRuby, which runs on the JVM, and IronRuby, which runs on the .Net platform.
  16. &amp;#x201C;How you feel is more important than what you do. &amp;#x201C; The entire language is designed for programmer productivity and fun.
  17. Principle of Least Surprise - This means The language should behave in a way that is not confusing to experienced developers. It doesn&amp;#x2019;t mean that it works like your current favorite language! But as you get used to Ruby, you&amp;#x2019;ll find that you ramp up quickly.
  18. Ruby achieves this through a consistant API. You won&amp;#x2019;t find yourself guessing too much what methods are available to you.
  19. It also helps that the syntax is simple. There are no unnecessary semicolons or curly braces. The interpreter knows when lines end.
  20. We have numbers, strings, multiplication, addition, subtraction, and division, just like everyone else.
  21. The square brackets denote an array.
  22. This is the hash symbol, or the hash rocket. Whenever you see this, you&amp;#x2019;re dealing with a hash.
  23. When you see these, you&amp;#x2019;re looking at Symbols. They represent names and some strings. They conserve memory, as repeating a symbol in your code uses the same memory reference, whereas repeating a string creates a new object on each use.
  24. Unless is an alias for &amp;#x201C;if not&amp;#x201D;. Subtle, but sometimes much more readable.
  25. You can append these suffixes to statements to prevent them from firing. This is a great space saver and it&amp;#x2019;s easy to read
  26. The two arrows (&gt;&gt;) is actually a method on the Date object that adds months. So here, we&amp;#x2019;re adding six months to the start date and comparing it to today Notice here that the input parameter is assumed to be a date. There&amp;#x2019;s no type checking here.
  27. The = is part of the method name. And Ruby&amp;#x2019;s interpreter doesn&amp;#x2019;t mind you putting a space in front of it to make it easier to read!
  28. Making getters and setters is so common that Ruby can do it for you.
  29. It assumes you are an intelligent person who wants to get things done. It will not try to protect you from your own stupidity.
  30. In fact,
  31. Test All The Effing Time! Let&amp;#x2019;s go through adding our &amp;#x201C;on_probation?&amp;#x201D; method to our Person class. A person is on probation for the first six months of employment.
  32. Here we have two tests, one using a person hired today, and another using a person last year.
  33. Did we miss any cases?
  34. Everything is an object in Ruby. There are no primitive types. Strings, integers, floats, everything. Even Nil, True, and False!
  35. Everything. Even 0 and -1.
  36. I&amp;#x2019;m not here to tell you that dynamically typed languages are better than statically typed languages. I prefer dynamic typing. I am more productive with it. And most of the claims against it are false.
  37. We don&amp;#x2019;t need to specify a &amp;#x201C;return&amp;#x201D; keyword.
  38. In this example, if the status is not closed, this method will return false. In Rails, if a before_save method returns false, the record won&amp;#x2019;t save to the database.
  39. There are methods on arrays and hashes to iterate over the elements stored within.
  40. Blocks let you pass code as a parameter, so that the code may be run within the method. If you&amp;#x2019;ve used closures or anonymous functions, you already understand this. But this is how Ruby developers work every day.
  41. We can create modules of code that we can mix in to our classes.
  42. In this example, we&amp;#x2019;re using modules to replace inheritence. However, since classes are objects, we can also apply modules to instances of objects at runtime.
  43. If it walks like a duck, and talks like a duck, it&amp;#x2019;s a duck. Even if it&amp;#x2019;s not.
  44. Declare modules that encapsulate behavior. Here we have a doctor, a ninja, and a musician.
  45. We can then mix in the behaviors to the instance of the class. Its type doesn&amp;#x2019;t really matter. We can ask the instance if it has the methods we want and we can call them.
  46. Reflection is built into the core language. It&amp;#x2019;s not a tacked on library, and it&amp;#x2019;s meant to be used to improve your code.
  47. We can ask our model all sorts of questions, and even actually send messages dynamically.
  48. We can loop over an array and generate methods on the object.
  49. Sinatra is a simple web framework that basically maps incoming requests to backend code that produces responses.
  50. That little bit of code gets us a working web application that handles requests.
  51. We write stories using plain text, that describes what we want to do.