SlideShare a Scribd company logo
Ruby on Rails
Ruby
(shorter now)
Everything is an object

  1.class         # => Fixnum
  'a'.class       # => String
  :z.class        # => Symbol

  class Foo
  end

  Foo.class       # => Class
  Foo.new.class   # => Foo
Dynamically typed
def do_stuff(thing)
  thing.do_the_stuff
end

class TheThing
  def do_the_stuff
    puts "Stuff was done!"
  end
end

do_stuff(TheThing.new)
Arrays


Array.new      # => []
Array.new(3)   # => [nil, nil, nil]
[]             # => []

a = [1,2,3]    #   =>   [1, 2, 3]
a[0] = 'one'   #   =>   "one"
a              #   =>   ["one", 2, 3]
a[-1]          #   =>   3
a[1..2]        #   =>   [2, 3]
Hashes

Hash.new              # => {}
{}                    # => {}

h = {1 => "one", 2 => "two"}
h[1]                 # => "one"
h["1"]               # => nil

h[:one] = "einz"     # => "einz"
h[:one]              # => "einz"

h.keys               # => [1, 2, :one]
h.values             # => ["one", "two", "einz"]
Classes
Classes
class Pet
  attr_accessor :name

  def initialize(name)
    @name = name
  end

  def sit!
    puts "Wuff"
  end
end
Naming conventions



CamelCased       # Classes, modules
with_underscores # methods, local variables

@instance_variable
@@class_variable
$GLOBAL_VARIABLE
Code blocks
Code blocks
[1,2,3,5,7].select {|x| x.even?}
# => [2]
Code blocks
[1,2,3,5,7].select {|x| x.even?}
# => [2]

def do_random_nr_of_times &block
  nr = rand(10)
  for i in 1..nr
    yield
  end
end
Code blocks
[1,2,3,5,7].select {|x| x.even?}
# => [2]

def do_random_nr_of_times &block
  nr = rand(10)
  for i in 1..nr
    yield
  end
end

do_random_nr_of_times { puts "bla" }
# bla
# bla
# bla
# => 1..3
Optional language
Optional language

def my_method(data, options = {})
  # ...
end
Optional language

def my_method(data, options = {})
  # ...
end

# Full signature
my_method("bla", {:option => 'value', :two => 2})
Optional language

def my_method(data, options = {})
  # ...
end

# Full signature
my_method("bla", {:option => 'value', :two => 2})
# The last parameter a hash? implicit.
my_method("bla", :option => 'value', :two => 2)
Optional language

def my_method(data, options = {})
  # ...
end

# Full signature
my_method("bla", {:option => 'value', :two => 2})
# The last parameter a hash? implicit.
my_method("bla", :option => 'value', :two => 2)
# Parentheses are optional
my_method "bla", :option => 'value', :two => 2
Optional language

def my_method(data, options = {})
  # ...
end

# Full signature
my_method("bla", {:option => 'value', :two => 2})
# The last parameter a hash? implicit.
my_method("bla", :option => 'value', :two => 2)
# Parentheses are optional
my_method "bla", :option => 'value', :two => 2

# As long as its unambiguous, it's OK
Ruby on Rails
Our case: a webshop
Our case: a webshop
What has rails built?
What has rails built?


• config
• db
• public
• script
• test
REST in Rails
REST in Rails
# Listing           # Reading
# GET /things       # GET /things/1
def index           def show
REST in Rails
# Listing               # Reading
# GET /things           # GET /things/1
def index               def show


# Creating              # Updating
# GET /things/new       # GET /things/1/edit
def new                 def edit
# POST /things          # PUT /things/1
def create              def update
REST in Rails
# Listing                  # Reading
# GET /things              # GET /things/1
def index                  def show


# Creating                 # Updating
# GET /things/new          # GET /things/1/edit
def new                    def edit
# POST /things             # PUT /things/1
def create                 def update


# Deleting
# DELETE /things/1
# Actually: POST /things/1 with :destroy = true
def destroy
Layouts
Layouts
Filters
The Shop controller
Partials

/things/_my_partial.html.erb
<div id="my_div">
    <p>Hello, <%= person %>!</p>
</div>
Partials

 /things/_my_partial.html.erb
 <div id="my_div">
     <p>Hello, <%= person %>!</p>
 </div>




<%= render
    :partial => 'my_partial',
    :locals => {:person => 'angelo'} %>
Partials
Partials
The Cart
The Cart
REST in Rails
# Listing                  # Reading
# GET /things              # GET /things/1
def index                  def show


# Creating                 # Updating
# GET /things/new          # GET /things/1/edit
def new                    def edit
# POST /things             # PUT /things/1
def create                 def update


# Deleting
# DELETE /things/1
# Actually: POST /things/1 with :destroy = true
def destroy
REST in Rails
# Listing                  # Reading
# GET /things              # GET /things/1
def index                  def show


# Creating                 # Updating
# GET /things/new          # GET /things/1/edit
def new                    def edit
# POST /things             # PUT /things/1
def create                 def update


# Deleting
# DELETE /things/1
# Actually: POST /things/1 with :destroy = true
def destroy
Line and Order
What was all that?
What was all that?



• Convention over configuration
What was all that?



• Convention over configuration
• Mixed languages as necessary
What was all that?



• Convention over configuration
• Mixed languages as necessary
• Rapid development
Resources




http://pragprog.com/
          titles/rails2
Resources




http://www.railscasts.com
What haven’t I talked
What haven’t I talked




• AJAX-y things
What haven’t I talked




• AJAX-y things
• Testing (RSpec)

More Related Content

KEY
Device deployment
KEY
Massive device deployment - EclipseCon 2011
KEY
Evolving systems and the link to service orientation
PDF
New BYOB Law for Sayreville (The Suburban)
PDF
Танки_в_Лунапарке: нагрузочное_тестирование_в_Яндексе
PDF
Postman & Python: Harmonious Development
PDF
Csharp Intsight
PDF
Marketing Digital - parte 3
Device deployment
Massive device deployment - EclipseCon 2011
Evolving systems and the link to service orientation
New BYOB Law for Sayreville (The Suburban)
Танки_в_Лунапарке: нагрузочное_тестирование_в_Яндексе
Postman & Python: Harmonious Development
Csharp Intsight
Marketing Digital - parte 3

What's hot (19)

PDF
InnoDB Magic
PDF
ApacheCon NA11 - Apache Celix, Universal OSGi?
PDF
323 n ministerial
PDF
Time Travel - Predicting the Future and Surviving a Parallel Universe - JDC2012
PDF
[Harvard CS264] 04 - Intermediate-level CUDA Programming
PDF
Self Review and Personal Growth
PDF
Noung — Snakes of the Tonle Sap
PDF
OSGI workshop - Become A Certified Bundle Manager
PDF
what’s wrong with the philippine higher education
PDF
Changing climate change before it changes us
PDF
NRI Report
PDF
Robert Murdock Band Brings 60s back to S.A. (The Suburban)
PDF
Max Niederhofer, Qwerly
PDF
powersky
PDF
Provincia Germán Busch
PDF
Chemistry for composites powering aerospace industry - Highlight
PDF
PDF
illustration art market report illustrated gallery
PDF
Recommender Systems [Borsani, Camedda, Leo]
InnoDB Magic
ApacheCon NA11 - Apache Celix, Universal OSGi?
323 n ministerial
Time Travel - Predicting the Future and Surviving a Parallel Universe - JDC2012
[Harvard CS264] 04 - Intermediate-level CUDA Programming
Self Review and Personal Growth
Noung — Snakes of the Tonle Sap
OSGI workshop - Become A Certified Bundle Manager
what’s wrong with the philippine higher education
Changing climate change before it changes us
NRI Report
Robert Murdock Band Brings 60s back to S.A. (The Suburban)
Max Niederhofer, Qwerly
powersky
Provincia Germán Busch
Chemistry for composites powering aerospace industry - Highlight
illustration art market report illustrated gallery
Recommender Systems [Borsani, Camedda, Leo]
Ad

Similar to Rails by example (20)

PDF
Ruby on Rails
PDF
Ruby on Rails at PROMPT ISEL '11
PDF
Ruby 入門 第一次就上手
PPT
Ruby on Rails
KEY
Refactor like a boss
KEY
PDF
Designing Ruby APIs
PPTX
Code for Startup MVP (Ruby on Rails) Session 2
PDF
Story for a Ruby on Rails Single Engineer
PDF
Slides chapter3part1 ruby-forjavaprogrammers
PDF
Ruby on Rails - Introduction
KEY
Desarrollando aplicaciones web en minutos
PDF
Rails workshop for Java people (September 2015)
PDF
Ruby and Rails by Example (GeekCamp edition)
KEY
A tour on ruby and friends
KEY
Intro to Ruby on Rails
PDF
Active Support Core Extensions (1)
PDF
Ruby talk romania
KEY
An introduction to Ruby
KEY
Rails Presentation (Anton Dmitriyev)
Ruby on Rails
Ruby on Rails at PROMPT ISEL '11
Ruby 入門 第一次就上手
Ruby on Rails
Refactor like a boss
Designing Ruby APIs
Code for Startup MVP (Ruby on Rails) Session 2
Story for a Ruby on Rails Single Engineer
Slides chapter3part1 ruby-forjavaprogrammers
Ruby on Rails - Introduction
Desarrollando aplicaciones web en minutos
Rails workshop for Java people (September 2015)
Ruby and Rails by Example (GeekCamp edition)
A tour on ruby and friends
Intro to Ruby on Rails
Active Support Core Extensions (1)
Ruby talk romania
An introduction to Ruby
Rails Presentation (Anton Dmitriyev)
Ad

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
A Presentation on Artificial Intelligence
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation theory and applications.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
cuic standard and advanced reporting.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Electronic commerce courselecture one. Pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Empathic Computing: Creating Shared Understanding
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Advanced methodologies resolving dimensionality complications for autism neur...
A Presentation on Artificial Intelligence
Spectral efficient network and resource selection model in 5G networks
Encapsulation theory and applications.pdf
Approach and Philosophy of On baking technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Understanding_Digital_Forensics_Presentation.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
cuic standard and advanced reporting.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Electronic commerce courselecture one. Pdf
Big Data Technologies - Introduction.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MYSQL Presentation for SQL database connectivity
Review of recent advances in non-invasive hemoglobin estimation
Empathic Computing: Creating Shared Understanding

Rails by example

Editor's Notes

  • #4: Symbol can be seen as a constant without any overhead. Used for indexing, as key, etc.
  • #33: Greatly increases readability!
  • #34: Greatly increases readability!
  • #35: Greatly increases readability!
  • #36: Greatly increases readability!
  • #37: Greatly increases readability!
  • #38: Greatly increases readability!
  • #39: Greatly increases readability!
  • #40: Greatly increases readability!
  • #41: Greatly increases readability!
  • #42: Greatly increases readability!
  • #43: I have been doing Ruby on Rails for about six weeks now; time for a quick introduction! This is not intended as a be-all-and-end-all presentation; I point out the things I find interesting or surprising. Image: http://shakti.trincoll.edu/~dmerrick/images/ruby.png