Transcript PPT

Introduction to Ruby
Introduction
1.
2.
3.
4.
5.
Ruby Basics
Ruby for the Web
Ruby on Rails
Demo
Questions
What is Ruby?
• Invented in mid-1990s by Yukihiro “Matz”
Matsumoto
• Interpreted Language
• Completely Object Oriented
• Built to be FUN!
(Play with it at http://repl.it/languages/Ruby)
Variables & Strings
• Local variables don’t require definition.
• Anything in a string surrounded by #{} allows
for what’s in the brackets to be evaluated
before being added to the string. This is string
interpolation.
Conditionals and comparisons
Arrays
• Arrays are objects like everything else.
• All their functions can be seen online.
Hash
• Like ‘associative arrays’ for PHP. Stores a value
with a key.
Loops
• For loops aren’t commonly used. #each and
#times provides a more readable alternative
• The variable in the pipes is the current value
it’s using. This is block syntax – you’ll see it
more.
Iteration
• This is how most iteration is done. #each will
run what’s between do/end as many times as
there are elements, setting the variable in the
brackets to the current element.
Object Oriented
• EVERYTHING in Ruby is an object. (even classes)
• All classes are open to redefinition – even core!
Defining a New Class
• All class names must be CamelCased.
Extendibility
• Ruby’s greatest strength is extendibility.
• There are “gems” (plugins) available for
anything you could think of needing.
• Install the gem, require it in your code, and
you get all its functionality!
• 69,880 gems made since July 2009.
(http://rubygems.org)
Ruby on the Web
• Ruby was just a scripting language.
• Programmers wrote frameworks to write Ruby
for the web (because it’s fun!)
• Popular frameworks include Ruby on Rails and
Sinatra.
Ruby on Rails
• Framework written by David Heinemeier
Hansson and released in 2004.
• Open source project
(http://github.com/rails/rails)
• Written to very rapidly create database
backed applications.
• Very opinionated – but these opinions have
been built by fantastic engineers.
Making a Rails Application
Run the below commands in a terminal when
Ruby is installed.
1. gem install rails
2. rails new blog
3. cd blog
4. rails server (a server is built into Rails for
development)
5. Visit http://localhost:3000
6. Voila – a web server running on ruby!
Rails – (M)odel-(V)iew-(C)ontroller
• Rails is an MVC framework – meaning it
separates application logic into three parts,
the Model, the View, and the Controller
Rails – Models
• Models are object representations of items
stored in the database.
• There is usually one per “thing” – a table is a
model, a chair is a model, etc.
• Best practice is to store information here and
do nothing else – there shouldn’t be a lot of
logic in the model.
Rails – Controllers
• Controllers control the interaction between all
aspects of the application and decides which
view to show.
• Demo – rails g controller welcome index
• This creates a “welcome” controller with the
index action.
Rails - Routes
• Routes decide which URL paths navigate to
which controllers.
• Root :to => “welcome#index” says that the
root path goes to the welcome controller,
index action.
Rails - Views
• Views contain the HTML that get rendered.
• The index action of the welcome controller
with render the HTML in
app/views/welcome/index.html.erb.
Rails - Scaffolding
• Rapidly create the common
Create/Read/Update/Modify views for a
database item.
• Rails generate scaffold Post title:string
description:text
• Rake db:migrate (This updates the database)
Scaffolding – Part 2
• Scaffolding is usually frowned upon – it builds
a lot of framework for actions you may not
need.
• However, for now, it creates a lot for little –
feel free to play with it!
Pros/Cons of Rails
1. Pros
1. Very fast development
2. Amazing toolsets – some of the best testing and
debugging tools available to web developers.
2. Cons
1. Hardware. Ruby and Ruby on Rails make an
important decision – developer time is worth
more than the cost of hardware.
Questions?
Contact Me
Get a hold of me if you’d like to talk!
Email: [email protected]
Github: terrellt
Check out our source code:
http://github.com/osulp