RUBY ON RAILS
   the beginning
ANY IDEAS?
RUBYONRAILS.ORG
RUBY-LANG.ORG
PROGRAMMING
  LANGUAGE    WEB FRAMEWORK
Ruby on Rails is an open-source
web framework that’s optimized
for programmer happiness and
sustainable productivity.

It lets you write beautiful code
by favoring convention over
configuration.
Ruby on Rails is an open-source
web framework that’s optimized
for programmer happiness and
sustainable productivity.

It lets you write beautiful code
by favoring convention over
configuration.
Ruby on Rails is an open-source
web framework that’s optimized
for programmer happiness and
sustainable productivity.

It lets you write beautiful code
by favoring convention over
configuration.
Ruby on Rails is an open-source
web framework that’s optimized
for programmer happiness and
sustainable productivity.

It lets you write beautiful code
by favoring convention over
configuration.
Ruby on Rails is an open-source
web framework that’s optimized
for programmer happiness and
sustainable productivity.

It lets you write beautiful code
by favoring convention over
configuration.
80/20
 Pareto principle
DON’T
REPEAT
YOURSELF
DON’T
REPEAT
YOURSELF
DUPLICATION
IS
EVIL
DUPLICATION
IS
EVIL
RUBY
Why an other
programming
  language?
Some might like this, but it's
 mostly because most Software
Development Tools are designed
 for computers, not for human
            beings.
Some might like this, but it's
 mostly because most Software
Development Tools are designed
 for computers, not for human
            beings.
Some might like this, but it's
 mostly because most Software
Development Tools are designed
 for computers, not for human
            beings.
“Ruby is simple in
appearance, but is
very complex inside,
just like human body”

Yukihiro “Matz” Matsumoto,
Creator of Ruby
MAKE THE
 COMPUTER
WORK FOR YOU
DESIGN THE
LANGUAGE AND
 LIBRARIES FOR
  PEOPLE FIRST
LET OTHERS
 CARE ABOUT
PERFORMANCE
say = “I love Ruby”
puts say

=> “I love Ruby”
say = “I love Ruby”
say[‘love’] = “*love*”
puts say.upcase

=> “I *LOVE* RUBY”
4.times { puts say }

=>   “I   *love*   Ruby”
=>   “I   *love*   Ruby”
=>   “I   *love*   Ruby”
=>   “I   *love*   Ruby”
Time.now + 1.day

=> Fri Jul 30 17:48:05 +0300 2010
Ruby is a dynamic, open source
programming language with a focus
on simplicity and productivity.

It has an elegant syntax that is
natural to read and easy to write.
Ruby is a dynamic, open source
programming language with a focus
on simplicity and productivity.

It has an elegant syntax that is
natural to read and easy to write.
Ruby is a dynamic, open source
programming language with a focus
on simplicity and productivity.

It has an elegant syntax that is
natural to read and easy to write.
Ruby is a dynamic, open source
programming language with a focus
on simplicity and productivity.

It has an elegant syntax that is
natural to read and easy to write.
Ruby is a dynamic, open source
programming language with a focus
on simplicity and productivity.

It has an elegant syntax that is
natural to read and easy to write.
Ruby is a dynamic, open source
programming language with a focus
on simplicity and productivity.

It has an elegant syntax that is
natural to read and easy to write.
Ruby is a dynamic, open source
programming language with a focus
on simplicity and productivity.

It has an elegant syntax that is
natural to read and easy to write.
LESS IS MORE
JAVA
public static String reverseString(String source) {
   int i, len = source.length();
   StringBuffer dest = new StringBuffer(len);
}
for (i = (len - 1); i >= 0; i--)
   dest.append(source.charAt(i));
return dest.toString();

reverseString("Krawutzikapuzi");

=> "izupakiztuwarK"
PHP

strrev("Krawutzikapuzi");

=> "izupakiztuwarK"
RUBY

"Krawutzikapuzi".reverse

=> "izupakiztuwarK"
RUBY


"Krawutzikapuzi".split(//).inject(""){ |m,c| c + m }

=> "izupakiztuwarK"




       Not using the built-in “reverse” method. Still much smaller that Java.
COMPLETE
  OBJECT
ORIENTATION
10.class

=> Fixnum
Fixnum.object_id

=> 108610
(10*10).class

=> Fixnum
(10*10*10*100000).class

=> Bignum
class String
	 def method_missing(method)
	 	 puts "intercepting call to #{method}"
	 end
end

"Lorem ipsum dolor".krawutzikaputzi

=> "intercepting call to krawutzikaputzi"
>> Post.find_by_published(true)

=> #<Post id: 1, title: "Hallo FH!", body:
"Lorem ipsum dolor sit amet, consectetur
adipisicing...", published: true, created_at:
"2008-04-14 12:07:46", updated_at:
"2008-04-14 12:07:46">
>> Post.find_by_published(true)

=> #<Post id: 1, title: "Hallo FH!", body:
"Lorem ipsum dolor sit amet, consectetur
adipisicing...", published: true, created_at:
"2008-04-14 12:07:46", updated_at:
"2008-04-14 12:07:46">
RAILS
MVC
MVC
        (model-view-controller)




            Controller




Model                             View
MVC
        (model-view-controller)




            Controller




Model                             View
MVC
        (model-view-controller)




 DB                               Helper
            Controller




Model                             View
REST
IN THE BEGINNING
THERE WAS THE URL
http://pluspoker.true-vision.net/content/index.html
http://www.merriam-webster.com/cgi-bin/mwwod.pl
http://gilesbowkett.blogspot.com/search?updated-
max=2008-04-08T09%3A25%3A00-07%3A00&max-results=7
http://www.amazon.de/gp/ product/B0000262LH/ ref=s9subs_c3_img1-
rfc_p_19_32_31_9_9? pf_rd_m=A1IDDPBG1NC5TQ&pf_rd_
s=center-2&pf_rd_r=1FMGVYJN44 H7WQD9YCR9&pf_rd_t=101&pf_rd_
p=139524591&pf_rd_i=301128
REST
(Representational State Transfer)
WEB APPLICATION
USUALLY CONSISTS
   OF OBJECTS
(like, say, blog posts)
WHAT IS A BLOG POST?
BLOG POST



It’s an entry in a database.
Consists of title, body, date,
       and other data.
HTML REPRESENTATION




 http://0.0.0.0:3000/posts/1
XML REPRESENTATION
<?xml version=“1.0” encoding=“UTF-8”?>
<post>
   <id type=“integer”>1</id>
   <title>Another title</title>
   <body>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
   eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
   minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
   ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
   velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
   cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
   est laborum.</body>
   <created-at type=“datetime”>2008-04-14T12:07:46+02:00</created-at>
   <published type=“boolean”>true</published>
   <updated-at type=“datetime”>2008-04-15T11:30:50+02:00</updated-at>
   <version type=“integer”>2</version>
</post>




    http://0.0.0.0:3000/posts/1.xml
def show
  @post =
  Post.find(params[:id])respond_to do |
  format|
    format.html
    format.xml { render :xml => @post }
  end
end
RESTFUL OPERATIONS


      • create

      • read

      • update

      • delete
HTTP VERBS
in HTTP each request is preceeded
    by a request type, or “verb”.
Normal page loads use the “GET” verb,
   while web forms use “POST”.
HTTP knows more verbs, most
importantly “PUT” and “DELETE”.
REST operation   HTTP verb


   Create          POST
    Read            GET
   Update           PUT
   Delete         DELETE
GET   http://localhost:3000/posts/1
DELETE   http://localhost:3000/posts/1
GET     http://localhost:3000/posts
 GET     http://localhost:3000/posts/1
 POST     http://localhost:3000/posts
 PUT     http://localhost:3000/posts/1
DELETE    http://localhost:3000/posts/1
The best thing is
that Rails does it all
    completely
   automatically.
ROUTING
map.resources :posts
map.sign_up 'sign_up/', :controller => 'registrations', :action => 'new'




              GET http://localhost:3000/sign_up/
map.resources :videos do |videos|
  videos.resources :comments
  videos.like '/like', :controller => 'favorites', :action => 'create'
end

map.resources :poker_rooms, :member => { :move_up => :get, :move_down => :get }
map.resources :favorites
map.resources :users

map.sign_up 'sign_up/', :controller => 'registrations', :action => 'new'
map.edit_registration 'user/edit', :controller => 'registrations', :action => 'edit'
map.sign_in 'sign_in/', :controller => 'sessions', :action => 'new'
map.sign_out 'sign_out/', :controller => 'sessions', :action => 'destroy'

map.resources :registrations, :only => [ :create, :update, :destroy ]
map.resources :sessions, :only => [ :create, :destroy ]

map.root :controller => 'videos', :action => 'preview'
ORM
(OBJECT RELATIONAL MAPPING)
Post.find 1
Post.find 1

SELECT * FROM posts
 WHERE posts.id = 1
       LIMIT 1
Post.find_all_by_title ‘Hello World’


       SELECT * FROM posts
WHERE posts.title = ‘Hello World’
class Post < ActiveRecord::Base
  belongs_to :user
end




class User < ActiveRecord::Base
  has_many :posts
end
@user = User.first

SELECT * FROM users LIMIT 1



      @user.posts
 SELECT * FROM posts WHERE
     posts.user_id = 1
GENERATORS
rails pluspoker
script/generate model user username:string age:integer
script/generate controller users
script/generate migration add_active_to_users active:boolean
NEED MORE?
WRITE YOU OWN!
RAILS IS 100% RUBY-WRITTEN
MONGREL
MONGREL


• Very    lightweight

• Single   request server

• Ideal   for local use
Browser   Mongrel
SERVER SIDE
Mongrel



                   Mongrel
          Apache
Browser
           nginx
                   Mongrel



                   Mongrel
Thread



                               Thread



                               ...
          Apache   Passenger
Browser
           nginx   mod_rails
Q&A

Rails Presentation (Anton Dmitriyev)