RUBY ON RAILSIntroduction to Ruby
What is Ruby?
Ruby is: from Japan
Yukihiro “Matz” Matsumoto
Ruby is: a scripting language
“Ruby is simple in appearance, but is very complex inside, just like our human body.” – Matz
Ruby is: Object Oriented
“I wanted a scripting language that was more powerful than Perl, and more object-oriented
than Python.” – Matz
5.times { print "We *love* Ruby -- it's outrageous!" }
Ruby is: Flexible
class Numeric def plus(x) self.+(x) endendy = 5.plus 6# y is now equal to 11
Ruby has:Blocks
search_engines = %w[Google Yahoo MSN].map do |engine| "http://www." + engine.downcase + ".com" end# search_engines = [‘http://www.google.com’, http://www.yahoo.com’, ‘http://www.msn.com’]
Ruby is:Single Inheritance
class MyArray include Enumerableend
Ruby is:Pretty Easy to
Read
var could be a local variable.@var is an instance variable.
$var is a global variable.
What is Rails?
Rails is:A Web Application Framework
written in Ruby
What’s the advantage?
Functionality common to many web applications abstracted into a stable, tested
code base.
Rails leverages the RubyGem code packaging system to make installing and versioning
libraries of code exceedingly easy.
So why is Rails unique?
Convention Over Configuration approach to get you started quickly.
DRY principles and built-in testing to help ensure your code is lasting.
How easy is it, really?
Let’s do an example.
How is it Structured?
Model View ControllerModel=State, View=Response to User, Controller=Logic
How does that show up in Rails?
Model — ActiveRecordController—ActionController
View—ActionView
MVC in Webapps
ModelOO abstraction for RDB
Classes—TablesObjects—Rows
Queries & Operations—Class Methodsclass Client < ActiveRecord::Base has_one :address has_one :mailing_address has_many :ordersend
ViewTemplates for HTML documents
ERB—Embedded Ruby
ControllerInterface between user, model and view.
This is where you line everything up.
Let’s go back to the example
Assignment“Seems really pricey for a relatively simple
software like this. Someone write an opensource alternative? it looks like
something that can be thrown together in a weekend.”
— ktharavaad writing about Stack Overflow @ news.ycombinator.com
Rewrite Stack Overflow
Requirements: Questions, Responses, Users, VotingGo further if you feel ambitious.
Tools RequiredText editor,Terminal,
script/server,http://api.rubyonrails.org/,
http://www.ruby-doc.org/core/,http://guides.rubyonrails.org/,
script/console