Top Banner
Ruby and Rails An Introduction [email protected]
28

Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Apr 21, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Ruby and RailsAn Introduction

[email protected]

Page 2: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Agenda

The language – Ruby

The framework – Ruby On Rails

Demo

Resources

Questions

Page 3: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Ruby

Perl

Java

Eiffel

SmallTalk

RUBY

Lisp

Page 4: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Ruby

Dynamic, Interpreted Object oriented (True) Scripting, RegExp Platform independent Lots of library support

Page 5: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Idioms

Principle of least surprise Principle of succinctness Principle of least effort Convention Extensible

Page 6: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Examples

puts "The obligatory hello world script"

Page 7: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

More Examples

10.times do

puts Time.now.to_s

sleep 0.5

end

Page 8: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Examples - Blocks

def wish(to)

puts "Good Afternoon, #{to}"

end

["people", "friends", "all!"].each do |addressee|

wish addressee

end

Page 9: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Examples

ages = [15, 20, 30, 20, 10, 50, 35, 30]

puts ages.uniq.sort.reverse

puts ages - [30, 50]

Page 10: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Examplesclass Rectangle def initialize(l,b) @length = l @breadth = b end

def area @length * @breadth end

def perimeter 2*(@length + @breadth) endend

Page 11: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Examples

rectangle = Rectangle.new(10,5)

puts rectangle.area

# prints 50

puts rectangle.perimeter

# prints 30

Page 12: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything
Page 13: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

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

Page 14: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

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.

Page 15: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Architecture

Page 16: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Components

Action Controller Action View Active Record Action Mailer Active Resource Railties Active Support

Page 17: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

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

Page 18: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Demo

• File structure of a rails app

• MVC

• Database

• Code Walkthrough

Page 19: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

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

Page 20: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

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

Page 21: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Deployment

RAKE - Ruby’s Build System WEBrick- Server is packaged with app Preferred: Apache/mongrel Environments: development, test, production

Page 22: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Development Environment

• Eclipse – RadRails

• NetBeans

• ...

Syntax highlighting, debuggers, autocomplete, some amount of refactoring

Page 23: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

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

Page 24: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

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

Page 25: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

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

Page 26: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Resources - Ruby

http://www.ruby-lang.org http://www.rubycentral.com/book http://www.ruby-doc.org/

Page 27: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Resources - Rails

http://rubyonrails.org http://guides.rubyonrails.org/ http://railsbrain.com/ http://railscasts.com

Page 28: Ruby and Rails An Introductionkarthiksr/resources/IntroToRails.pdf · Rails in a nutshell Web development that doesn't hurt Open source web framework built in Ruby Includes everything

Questions?

[This ppt and code will be available at ~karthiksr]