This preview shows page 1-2-3-4-5-6-7-8-57-58-59-60-61-62-63-64-115-116-117-118-119-120-121-122 out of 122 pages.
95-733 Internet Technologies 1 Internet Technologies Ruby and Ruby on Rails95-733 Internet Technologies 2 Ruby on Rails Material for this presentation was taken from Sebesta (PWWW, course text) and “Agile Web Development with Rails” by Ruby, Thomas and Hansson, third edition.95-733 Internet Technologies 3 Notes on Ruby From Sebesta's "Programming The World Wide Web" Ø Designed in Japan by Yukihiro Matsumoto Ø Released in 1996 Ø Designed to replace Perl and Python Ø Rails, a web application development framework , was written in and uses Ruby Ø Ruby is general purpose but probably the most common use of Ruby is Rails Ø Rails was developed by David Heinemeier and released in 2004 Ø Basecamp (project management) is written in RoR95-733 Internet Technologies 4 Ø To get started install rbenv or RVM (Ruby Version Manager) Ø Use ri command line tool to browse documentation (e.g., ri Integer). Ø Use rdoc to create documentation (like Javadoc) Ø Ruby is a pure object-oriented language. Ø All variables reference objects. Ø Every data value is an object. Ø References are typeless. Ø All that is ever assigned in an assignment statement is the address of an object. Ø The is no way to declare a variable. Ø A scalar variable that has not been assigned a value has the value nil. General Notes on Ruby(1)95-733 Internet Technologies 5 Ø Three categories of data types - scalars, arrays and hashes Ø Two categories of scalars - numerics and character strings Ø Everything (even classes) is an object. Ø Numeric types inherit from the Numeric class Ø Float and Integer inherit from Numeric Ø Fixnum (32 bits) and Bignum inherit from Integer Ø All string literals are String objects Ø The null string may be denoted as " or as '’”. Ø The String class has over 75 methods General Notes on Ruby(2)95-733 Internet Technologies 6 Ø Ruby gems: “There is a gem for that”. Ø A ruby gem provides functionality. Ø May run on its own. A stand alone program. Rails is a gem. Ø May be included in your code with require: Ø require ‘aws/s3’ # to access Amazon Simple Storage Service Ø require is the same as the c language’s include. Ø How do you install a gem? From the command line enter: Ø gem install GEM_NAME (usually from http://rubygems.org) Ø gem install rails Ø gem install jquery-rails General Notes on Ruby(3)95-733 Internet Technologies 7 Interactive Environment $irb >> miles = 1000 => 1000 >> milesPerHour = 100 => 100 >> "Going #{miles} miles at #{milesPerHour} MPH takes #{1/milesPerHour.to_f*miles} hours" => "Going 1000 miles at 100 MPH takes 10.0 hours"95-733 Internet Technologies 8 More interactive Ruby $irb >> miles = 1000 => 1000 >> s = "The number of miles is #{miles}" => "The number of miles is 1000" >> s => "The number of miles is 1000"95-733 Internet Technologies 9 Non-Interactive Ruby Save as one.rb and run with ruby one.rb a = "hi" b = a puts a puts b b = "OK" puts a puts b Output ====== hi hi hi OK95-733 Internet Technologies 10 References are Typeless a = 4 puts a a = "hello" puts a Output ===== 4 hello95-733 Internet Technologies 11 C Style Escapes puts "Hello\nInternet\tTechnologies” Hello Internet Technologies95-733 Internet Technologies 12 Converting Case a = "This is mixed case." puts a.upcase puts a puts a.upcase! puts a THIS IS MIXED CASE. This is mixed case. THIS IS MIXED CASE. THIS IS MIXED CASE.95-733 Internet Technologies 13 Testing Equality(1) b = "Cool course" == "Cool course" # same content puts b b = "Cool course".equal?("Cool course") #same object puts b puts 7 == 7.0 # same value puts 7.eql?(7.0) # same value and same type Output ====== true false true false95-733 Internet Technologies 14 Testing Equality(2) a = "Ruby is cool." b = "Ruby is cool." c = b if a == b puts "Cool" else puts "Oops" end if c.equal?(b) puts "Too cool" else puts "Big Oops" end if c.equal?(a) puts "Way cool" else puts "Major Oops" end $ruby test.rb Cool Too cool Major Oops What’s the output?95-733 Internet Technologies 15 Reading The Keyboard puts "Who are you?" name = gets #include entered newline name.chomp! #remove the newline puts "Hi " + name + ", nice meeting you." Interaction =========== Who are you? Mike Hi Mike, nice meeting you.95-733 Internet Technologies 16 Reading Integers #to_i returns 0 on strings that are not integers puts "Enter two integers on two lines and I'll add them" a = gets.to_i b = gets.to_i puts a + b Interaction =========== Enter two integers on two lines and I'll add them 2 4 695-733 Internet Technologies 17 Conditions with if a = 5 if a > 4 puts "Inside the if" a = 2 end puts "a == " + a.to_s(10) Output ====== Inside the if a == 295-733 Internet Technologies 18 Conditions with unless a = 5 unless a <= 4 puts "Inside the if" a = 2 end puts "a == " + a.to_s(10) Output ====== Inside the if a == 295-733 Internet Technologies 19 Conditions with if else a = 5 if a <= 4 puts "Inside the if" a = 2 else puts "a == " + a.to_s(10) end Output ====== a == 595-733 Internet Technologies 20 Conditions with if/elsif/else a = 5 if a <= 4 puts "Inside the if" a = 2 elsif a <= 9 puts "Inside the elsif" else puts "Inside else” end Output ===== Inside the elsif95-733 Internet Technologies 21 Conditions with case/when a = 5 case a when 4 then puts "The value is 4" when 5 puts "The value is 5" end Output ====== The value is 595-733 Internet Technologies 22 Conditions with case/when/else a = 2 case a when 4 then puts "The value is 4" when 5 puts "The value is 5" else puts "OK" end Output ====== OKStatement Modifiers 95-733 Internet Technologies 23 Suppose the body of an if or while has a single statement. Then, you may code it as: puts "This is displayed" if 4 > 3 j = 0 puts j+1 if j == 0 j = j + 1 while j < 100 puts j This is displayed 1 10095-733 Internet Technologies 24 Case/When with Range a = 4 case a when 4 then # after a match we are done puts "The value is 4" when (3..500) puts "The value is between 3 and 500" else puts "OK" end Output ====== The value is 495-733
View Full Document