240 likes | 593 Views
RUBY ON RAILS. Introduction 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.
E N D
RUBY ON RAILS • Introduction to 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) • end • end • y =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 Enumerable • end
Ruby is:Pretty Easy to Read • var could be a local variable. • @var is an instance variable. • $var is a global variable.
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 Controller • Model=State, View=Response to User, Controller=Logic
How does that show up in Rails? • Model — ActiveRecord • Controller—ActionController • View—ActionView
Model • OO abstraction for RDB • Classes—Tables • Objects—Rows • Queries & Operations—Class Methods • class Client < ActiveRecord::Base • has_one :address • has_one :mailing_address • has_many :orders • end
View • Templates for HTML documents • ERB—Embedded Ruby
Controller • Interface between user, model and view. • This is where you line everything up.
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, Voting • Go further if you feel ambitious.
Tools Required • Text editor, • Terminal, • script/server, • http://api.rubyonrails.org/, • http://www.ruby-doc.org/core/, • http://guides.rubyonrails.org/, • script/console