Ruby on Rails - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Ruby on Rails

Description:

Ruby on Rails What's Ruby A programming language Developed by Yukihiro Matsumoto (aka Matz) in the 1990s What's Rails Initially developed by David Heinemeier Hansson ... – PowerPoint PPT presentation

Number of Views:208
Avg rating:3.0/5.0
Slides: 40
Provided by: wwwxCsUn6
Category:
Tags: rails | ruby

less

Transcript and Presenter's Notes

Title: Ruby on Rails


1
Ruby on Rails
2
What's Ruby
  • A programming language
  • Developed by Yukihiro Matsumoto (aka Matz) in the
    1990s

3
What's Rails
  • Initially developed by David Heinemeier Hansson,
    out of his work on Basecamp, a project management
    system
  • It is a framework of scripts in ruby that provide
    for rapid development of web applications, esp
    those with a database back end
  • Rails can build the skeleton of an application,
    including the database tables, in just a few
    commands

4
Ruby
5
Syntax
  • Ruby is largely and loosely based on perl (hence
    the name, according to lore)
  • Completely object oriented

6
Some Important differences
  • Unlike PHP, scope in variables is defined by the
    leading sigil
  • the sign denotes global scope, not a variable
  • an _at_ represents local scope within an object
    instance
  • _at__at_ represents local scope within a class
  • A capitalized name is a constant

7
Historical Differences
  • Javascript--born of the competition between two
    companies
  • PHP--created by a varied community
  • Ruby--the vision of a single person
  • Rails--the vision of another single person
  • When you compare these, you can see how the
    starting point influences the process of
    development

8
Playing on the Command Line
  • Ruby is an interpreter, just like php or
    bashAvatar hays rubyprint "howdy world!"d
  • Or, use ruby -e "command"ruby -e 'puts
    "hello\n"'
  • Or, you can just use irb, which is
    easierAvatar hays irbgtgt print "howdy
    world!"howdy world!gt nilgtgt

9
Object Oriented
  • Truly
  • Not a prototyping language like javascript
  • Nor a procedural language with OOP bolted on

10
Classes
  • A class is a kind of master object
  • Can contain constants and methods
  • Instances of object can be created from a class,
    inheriting the traits of the class

11
A simple class
  • class Cat
  • end
  • (but this class doesn't do or mean anything)

the class examples are derived from
http//www.juixe.com/techknow/index.php/2007/01/2
2/ruby-class-tutorial/
12
cat class
  • I want four attributes for a cat name, color,
    type, and attribute

class Cat must be capitalized attr_accessor
name, type, color, attribute def
initialize(name, type, color, attribute)
_at_name name _at_type type _at_color
color _at_attribute attribute end
13
creating a new cat
  • Now, I can create an instance of the cat
    classgc Cat.new("GC", "short hair",
    "black", "gimpy")lc Cat.new("LC", "short
    hair", "black", "little")

14
add a method
  • I'd like to be able to describe my cats easily
  • So I add a method to the cat class def
    describe _at_name " is a " _at_color " "
    _at_type " who is " _at_attribute
    ".\n" end

15
eliminating con-cat-ination
  • The concatenation is a bit awkward
  • Like php, ruby has a structure for calling
    variables within a string"_at_name is a
    _at_color _at_type who is _at_attribute.\n"

16
calling the method
  • If I call a cat with the describe method
    attached, I can get the description of that cat
    my_string gc.describe puts my_string
  • or puts gc.describe

17
finding cats by name
  • A second method, find_by_namedef
    self.find_by_name(name)
  • found nil
  • ObjectSpace.each_object(Cat) o
  • found o if o.name name
  • found
  • end

18
Access Control
  • Methods in a class are public by default
  • Private methods are known only to the individual
    object
  • Protected methods can only be called by members
    of the class in which is was defined

19
Variables
  • In ruby, vars are references to objects, not
    objects themselves
  • Soa "my value"b a
  • a0 "n"
  • will change both a and b--but if you reassign
    a, eg a"new value", a is linked to a new object
    (this might bite you, but it's not likely)

20
Arrays
  • Create an array by assignmentmy_array
    "one", "two", 3, 4
  • Referencing the arrayputs "my_array0 is
    my_array0\n"
  • The brackets are methods of the array class

21
Hashes
  • What in php is called an associative array is
    called a hash in ruby
  • Creating a hash by assignmentmy_hash 'tree'
    gt 'pine', 'bird' gt 'mocking'puts "\n"puts
    "my_hash'tree' is my_hash'tree'\n"puts
    "my_hash'bird' is my_hash'bird'\n"
  • Notice that the syntax is different

22
walking a hash or array
  • use the each method

a 1 my_hash.each do key, value puts "a
key is value" a a 1 end
23
conditional
  • much like php and javascript, but simpler syntax

a 1 my_hash.each do key, value if key
"tree" puts "a key is value"
end a a 1 end
24
In summary
  • Ruby's syntax is pretty
  • Ruby is all about structure
  • Classes are easy to work with, if you're new,
    start with simple examples

25
Rails
26
Model View Controller (MVC)
  • Layering again
  • MVC allows a project team to work on different
    aspects of the application without stepping on
    each other's toes quite so often
  • Note that neither PHP nor Javascript encourage
    this, but it can be done in PHP (not so much in
    Javascript)
  • Rails enforces MVC

27
Model
  • Contains the data of the application
  • Transient
  • Stored (eg Database)
  • Enforces "business" rules of the application
  • Attributes
  • Work flow

28
Views
  • Provides the user interface
  • Dynamic content rendered through templates
  • Three major types
  • Ruby code in erb (embedded ruby) templates
  • xml.builder templates
  • rjs templates (for javascript, and thus ajax)

29
Controllers
  • Perform the bulk of the heavy lifting
  • Handles web requests
  • Maintains session state
  • Performs caching
  • Manages helper modules

30
Convention over Configuration
  • Notion that coding is reduced if we adopt a
    standard way of doing things
  • Eg., if we have a class "Pet" in our model that
    defines the characteristic of domestic animal, in
    rails, the database table created for us will be
    named "pets"
  • Other chunks of code look for each other by their
    common names

31
Action Pack
  • Since views and controllers interact so tightly,
    in rails they are combined in Action Pack
  • Action pack breaks a web request into view
    components and controller compoents
  • So an action usually involves a controller
    request to create, read, update, or delete (CRUD)
    some part of the model, followed by a view
    request to render a page

32
Processing URLs
  • The basic url used to access a controller is of
    the form http//server/controller/action
  • The controller will be one you generate, and the
    action will be one you've defined in your
    controller
  • So if you have a controller named "filer" and
    that controller has an action named "upload", the
    url will be something like http//127.0.0.1/filer/
    upload

33
The View
  • The controller will have a folder in app/view
    named after it, and in that will be the view
    templates associated with the action methods
  • These templates are usually html with some
    inserted ruby code
  • While code can be executed in these templates,
    keep that simple--any data controls should be
    made in the controller's files

34
Creating a basic site
  • Three commandsrails democd demoruby
    script/generate controller Bark
  • This creates the framework

35
Making it say something
  • A def in the app/controller/bark_controller.rb
    filedef helloend
  • And some html in the app/views/bark folder,
    hello.html.erb

lthtmlgtltheadgtlt/headgt ltbodygt lth3gtHowdylt/h3gt lt/bodygt
lt/htmlgt
36
Directory Structure
  • app most of your code lives here
  • config information environment and database link
  • database.yml
  • development, test and production versions
  • doc, log, tmp
  • lib your code, just a place to stick things that
    don't have a good home elsewhere

37
Directory Structure
  • public images, javascripts, stylesheets go here
  • script script that rails uses, most of these are
    short and reference files in the lib dir for
    rails
  • vendor 3rd party code

38
Generating a database site
  • Magic

rails temp cd temp rake dbcreateall ruby
script/generate scaffold Person lnamestring
fnamestring emailstringrake dbmigrate ruby
script/server
39
Sources
  • http//github.com/rails/rails/tree/master/actionpa
    ck
  • http//en.wikipedia.org/wiki/Ruby_on_Rails
  • http//www.whytheluckystiff.net/ruby/pickaxe/
  • http//www.pragprog.com/titles/rails3/agile-web-de
    velopment-with-rails-third-edition
  • http//www.juixe.com/techknow/index.php/2007/01/22
    /ruby-class-tutorial/
Write a Comment
User Comments (0)
About PowerShow.com