ruby on rails. what is ruby? programming language object-oriented interpreted

32
Ruby on Rails

Upload: tamsyn-johns

Post on 02-Jan-2016

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

Ruby on Rails

Page 2: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

What is Ruby?Programming LanguageObject-orientedInterpreted

Page 3: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

Interpreted LanguagesNot compiled like JavaCode is written and then directly executed

by an interpreterType commands into interpreter and see

immediate results

Computer

RuntimeEnvironmentCompilerCodeJava:

ComputerInterpreterCodeRuby:

Page 4: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

What is Ruby on Rails (RoR)Development framework for web

applications written in Ruby Used by some of your favorite sites!

Page 5: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

Advantages of a frameworkStandard features/functionality are built-inPredictable application organization

Easier to maintainEasier to get things going

Page 6: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

InstallationWindows

Navigate to: http://www.ruby-lang.org/en/downloads/

Scroll down to "Ruby on Windows"Download the "One-click Installer"Follow the install instructions

Include RubyGems if possible (this will be necessary for Rails installation later)

Mac/LinuxOS X 10.4 ships with broken Ruby! Go here…

http://hivelogic.com/articles/view/ruby-rails-mongrel-mysql-osx

Page 7: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

hello_world.rb

puts "hello world!"

Page 8: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

puts vs. print"puts" adds a new line after it is done analogous System.out.println()

"print" does not add a new lineanalogous to System.out.print()

Page 9: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

Running Ruby ProgramsUse the Ruby interpreter

ruby hello_world.rb“ruby” tells the computer to use the Ruby interpreter

Interactive Ruby (irb) consoleirb

Get immediate feedbackTest Ruby features

Page 10: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

Comments# this is a single line comment

=beginthis is a multiline commentnothing in here will be part of the code

=end

Page 11: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

Variables

Declaration – No need to declare a "type"

Assignment – same as in JavaExample: (like javascript)

x = "hello world" # Stringy = 3 # Fixnumz = 4.5 # Floatr = 1..10 # Range

Page 12: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

ObjectsEverything is an object.

Common Types (Classes): Numbers, Strings, Ranges

nil, Ruby's equivalent of null is also an object

Uses "dot-notation" like Java objectsYou can find the class of any variable

x = "hello"x.class String

You can find the methods of any variable or class

x = "hello"x.methodsString.methods

Page 13: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

String Literals“How are you today?”

Page 14: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

Objects (cont.)There are many methods that all Objects

haveInclude the "?" in the method names, it is

a Ruby naming convention for boolean methods

nil?eql?/equal? (3.eql?2)==, !=, ===instance_of?is_a?to_s

Page 15: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

NumbersNumbers are objectsDifferent Classes of Numbers

FixNum, Float3.eql?2 false-42.abs 423.4.round 33.6.round 43.2.ceil 43.8.floor 33.zero? false

Page 16: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

String Methods"hello world".length 11

"hello world".nil? false

"".nil? false

"ryan" > "kelly" true

"hello_world!".instance_of?String true

"hello" * 3 "hellohellohello"

"hello" + " world" "hello world"

"hello world".index("w") 6

Page 17: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

Operators and LogicSame as Java

* , / , +, - Also same as Java

"and" and "or" as well as "&&" and "||"Strings

String concatenation (+)String multiplication (*)

Page 18: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

Conditionals: if/elsif/else/endMust use "elsif" instead of "else if"Notice use of "end". It replaces closing

curly braces in JavaExample:

if (age < 35)puts "young whipper-snapper"

elsif (age < 105)puts "80 is the new 30!"

elseputs "wow… gratz..."

end

Page 19: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

Inline "if" statementsOriginal if-statement

if age < 105puts "don't worry, you are still young"

end

Inline if-statementputs "don't worry, you are still young" if age < 105

Page 20: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

for-loops for-loops can use ranges Example 1:

for i in 1..10puts i

end

Page 21: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

for-loops and rangesYou may need a more advanced range for your for-loop

Bounds of a range can be expressions

Example:for i in 1..(2*5)

puts iend

Page 22: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

while-loops

Cannot use "i++"Example:

i = 0while i < 5

puts ii = i + 1

end

Page 23: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

unless"unless" is the logical opposite of "if"

Example:unless (age >= 105) # if (age < 105)puts "young."

elseputs "old."

end

Page 24: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

untilSimilarly, "until" is the logical

opposite of "while"Can specify a condition to have the

loop stop (instead of continuing)Example

i = 0until (i >= 5) # while (i < 5), parenthesis not required

puts Ii = i + 1

end

Page 25: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

MethodsStructure

def method_name( parameter1, parameter2, …)

statementsend

Simple Example:def print_ryan

puts "Ryan"end

Page 26: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

ParametersNo class/type required, just name them!Example:

def cumulative_sum(num1, num2)sum = 0for i in num1..num2

sum = sum + iendreturn sum

end

# call the method and print the resultputs(cumulative_sum(1,5))

Page 27: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

ReturnRuby methods return the value of the last

statement in the method, so…def add(num1, num2)

sum = num1 + num2return sum

end

can becomedef add(num1, num2)

num1 + num2end

Page 28: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

User Input"gets" method obtains input from a userExample

name = getsputs "hello " + name + "!"

Use chomp to get rid of the extra lineputs "hello" + name.chomp + "!"

chomp removes trailing new lines

Page 29: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

Changing typesYou may want to treat a String a number or

a number as a Stringto_i – converts to an integer (FixNum)to_f – converts a String to a Floatto_s – converts a number to a String

Examples"3.5".to_i 3"3.5".to_f 3.53.to_s "3"

Page 30: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

ConstantsIn Ruby, constants begin with an UppercaseThey should be assigned a value at most

onceThis is why local variables begin with a

lowercaseExample:

Width = 5def square

puts ("*" * Width + "\n") * Widthend

Page 31: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

Week 1 AssignmentDo the Space Needle homework from 142 in

Rubyhttp://www.cs.washington.edu/education/courses/

cse142/08au/homework/2/spec.pdfDOES need to scale using a constant

Use syntax that is unique to Ruby whenever possible

Expected output can be found under the Homework 2 Section

http://www.cs.washington.edu/education/courses/cse142/08au/homework.shtml

Page 32: Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted

ReferencesWeb Sites

http://www.ruby-lang.org/en/ http://rubyonrails.org/

BooksProgramming Ruby: The Pragmatic

Programmers' Guide (http://www.rubycentral.com/book/)

Agile Web Development with RailsRails RecipesAdvanced Rails Recipes