Ruby and Rails
An Introduction
karthiksr@cse.iitb.ac.in
Agenda
The language – Ruby
The framework – Ruby On Rails
Demo
Resources
Questions
Ruby
SmallTalk
Lisp
RUBY Perl
Eiffel
Java
Ruby
Dynamic, Interpreted
Object oriented (True)
Scripting, RegExp
Platform independent
Lots of library support
Idioms
Principle of least surprise
Principle of succinctness
Principle of least effort
Convention
Extensible
Examples
puts "The obligatory hello world script"
More Examples
10.times do
puts Time.now.to_s
sleep 0.5
end
Examples - Blocks
def wish(to)
puts "Good Afternoon, #{to}"
end
["people", "friends", "all!"].each do |addressee|
wish addressee
end
Examples
ages = [15, 20, 30, 20, 10, 50, 35, 30]
puts ages.uniq.sort.reverse
puts ages - [30, 50]
Examples
class Rectangle
def initialize(l,b)
@length = l
@breadth = b
end
def area
@length * @breadth
end
def perimeter
2*(@length + @breadth)
end
end
Examples
rectangle = Rectangle.new(10,5)
puts rectangle.area
# prints 50
puts rectangle.perimeter
# prints 30
Rails in a nutshell
Web development that doesn't hurt
Open source web framework built in Ruby
Includes everything needed to create web apps
according to the Model-View-Control pattern
Opinionated software(!)
“Rails is the killer app for Ruby.”
Yukihiro Matsumoto, Creator of Ruby
Philosophy
DRY – “Don’t Repeat Yourself” – writing the same code
over and over again is a bad thing
Convention Over Configuration – Rails makes
assumptions about what you want to do and how you’re
going to do it, rather than letting you tweak every little thing
through endless configuration files.
The 80-20 Rule
REST is the best pattern for web apps – organizing your app
around resources and HTTP verbs is the fastest way to go.
Architecture
Components
Action Controller
Action View
Active Record
Action Mailer
Active Resource
Railties
Active Support
Demo
$ rails myblog
$ cd myblog
$ ./script/server
$ script/generate scaffold Post title:string content:text
$ rake db:migrate
$ ./script/server
CRUD, posts.xml
validates_presence_of :title, :content
validates_uniqueness_of :title
Demo
• File structure of a rails app
• MVC
• Database
• Code Walkthrough
Unit Testing
• Unit testing: rspec – BDD
Post.should have(:no).record
Post.create(“first”, “some contents”)
Post.create(“second”, “more contents”)
Post.should have(2).records
Integration Testing
new_session do |bob|
bob.goes_to_login
bob.goes_to_signup
bob.signs_up_with :name => "bob", :passwd =>”bob”
assert_response :success
end
Deployment
RAKE - Ruby’s Build System
WEBrick- Server is packaged with app
Preferred: Apache/mongrel
Environments: development, test, production
Development Environment
• Eclipse – RadRails
• NetBeans
• ...
Syntax highlighting, debuggers, autocomplete,
some amount of refactoring
Plugins
Very easy to write plugins
Think of reuse before writing any piece of
generic code
Most probably, someone has written a plugin
http://agilewebdevelopment.com/plugins
The Rails Community
The developer community around Rails is very
active, helpful and excited
Rails Wiki - http://wiki.rubyonrails.com/
Rails Mailing List – very active
Success Stories
• Twitter (http://www.twitter.com)
• Github (http://www.github.com)
• YellowPages (http://www.yellowpages.com)
• SlideShare (http://www.slideshare.net)
• Lots more! @
http://rubyonrails.org/applications
Resources - Ruby
http://www.ruby-lang.org
http://www.rubycentral.com/book
http://www.ruby-doc.org/
Resources - Rails
http://rubyonrails.org
http://guides.rubyonrails.org/
http://railsbrain.com/
http://railscasts.com
Questions?
[This ppt and code will be available at ~karthiksr]