Слайд 1 - SE-Wiki

Download Report

Transcript Слайд 1 - SE-Wiki

Introduction to Ruby&Rails
Yuri Veremeyenko
Monica Verma
Presentation Structure
1. Ruby
– Overview
– Syntax
2. Rails
–
–
–
–
Introduction
What makes Rails a good choice?
Typical Web Request Flow
ROR and MVC
3. On-spot project creation
– Basic application (blog)
– Scaffold, Migrations, Routing, etc.
4. Conclusions
Ruby - overview
• Originally from Japan, first public version
1995
• Multipurpose: from scripting to fully fledged
OO applications
• Current Version : 1.8.7
• Web Site: http://www.ruby-lang.org/
• Ruby in your web browser:
http://tryruby.hobix.com/
Ruby – syntax examples
• Everything is an object
255.times {|i| puts "Hello #{i}" }
• Arrays
a = [1, 'hi', 3.14, 1, 2]
a.reverse
a.each { |x| puts x }
• Hashes
h = {:water => 'wet', :fire => 'hot'}
h.each_pair do |key, value| puts "#{key} is #{value}"; end
• Ranges
(0..2).each {|i| puts i} #=> [0, 1, 2]
Ruby – syntax examples II
• Classes & Methods
class Person
attr_accessor :name, :age
def initialize(name, age)
@name, @age = name, age
end
end
p = Person.new("jay", 29)
p.name
>> "jay”
•Exceptions
raise ArgumentError,
"Illegal arguments!”
...
begin
# Do something
rescue
# Handle exception
end
Ruby – syntax examples III
• Returning multiple parameters
class MyClass < MyAbstractClass
def my_method
return 1, 2
end
end
a = MyClass.new
b, c = a.my_method
• Reflection
a = MyClass.new
a.public_methods
a.respond_to? "mymethod"
Ruby on Rails
• “an open source web framework that
optimizes for programmer happiness and
sustainable productivity”
• This translates to:
– Usage of MVC pattern
– Scripts and generators for common actions
– Use of Ruby's object nature
– DRY (don't repeat yourself)
– Support for most Web 2.0 fancy stuff
ROR: installation
• Ruby
– The ruby interpreter (1.8.6 recommended)
• Gem
– Gem is ruby package manager
• Database (postgres / mysql / sqlite)
– Install as you would normally
• Bind Ruby and your Database
– Normally available as gem
• Rails and other Gems and libs, as required
– Libopenssl-ruby, ruport, rdoc, irb, etc..
• IDE
What are typical activities
for web application?
•
•
•
•
•
Display form
Validate form
Display form errors
Save form
Show aggregated
data (and more
forms)
What makes ROR a good
choice?
•
•
•
•
•
•
MVC
ORM (ActiveRecord with associations)
Data validation standardized
Well structured application layout
Generators for common tasks
Testing support
ROR in typical web request
Fig.1 Web request flow
ROR and MVC
Fig.2 MVC in Rails
• Model : database logic, business data logic
• Controller : request processing, couples View and
Model
• View : sets of templates to render HTML
Generators and migrations
• Generators can create skeletons for
models/views/controllers/ or scaffold
>ruby script/generate scaffold MyEntity
• Migrations provide DB synchronization
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t| {
t.column :title, :string ;
t.column :body, :text }
end
def self.down
drop_table :posts ; end; end
Inside Rails
Fig.3 Rails Model code
Example Application
•
•
•
•
•
•
A sample blog application in 20 minutes
Create Posts and Comments
Scaffolding, migrations, rake
Specifying relationships in Models
Customizing generated Controllers
Very basic HTML
Conclusions
Ruby on Rails:
•
•
•
•
•
•
•
Is an MVC framework for Web application
Has efficient ActiveRecord ORM
Supports migrations
Has generators for common tasks
Supports unit testing
Provides AJAX support via prototype
RESTful since v2.0