DOC PREVIEW
Penn CIT 597 - Ruby

This preview shows page 1-2-3-4-27-28-29-30-55-56-57-58 out of 58 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 58 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 58 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 58 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 58 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 58 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 58 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 58 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 58 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 58 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 58 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 58 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 58 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 58 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Ruby“Hello World” in RubyEuclid’s algorithmGeneral principlesNumbersOther data typesOperatorsAssignment and alias statementsif statementsunless statementscase and ===Loops in RubyLoop controlsExceptionsBlocksDefining methodsDefining classesCalling methodsIteratorsSlide 20Example use of an iteratorString#each_charBlocks againSimplest use of yieldMy version of loopFibonacci numbersPassing a parameter to the blockReturning a value from a coroutineRegular expressionsArraysHashesAdding and removing methodsAttributes (instance variables)Shorthand for getters and settersAccess controlsevalprintf and friendsSome File < IO methodsSome File methodsStreamsSome String methodsSome more String methodsSome Array methodsChainingContextMore iteratorsProcsProcs are closures, tooProcs as parametersReflectionUndefined methodsAdding methods to a classModulesMetaprogrammingThe command lineLooking aheadTutorialsThe EndJan 13, 2019Ruby(Bet you can’t do this in Java!)“Hello World” in Rubyputs "Hello World!"Euclid’s algorithmdef euclid x, y while x != y if x > y then x -= y end if y > x then y -= x end end xendputs "Enter two numbers: "STDOUT.flushx = Integer(gets)y = Integer(gets)puts "The GCD of #{x} and #{y} is #{euclid x, y}“Enter two numbers: 24108The GCD of 24 and 108 is 12General principlesEverything is dynamic and may be changedAll values are objects (there are no “primitives”)Variables are typeless and need not be declaredRuby avoids unnecessary punctuationA statement on a line by itself needs no semicolonMultiple statements on a line are separated by semicolonsA line is continued if it ends in an operator, comma, or backslashParameters to a method don’t usually need parenthesesConditions (in if, while, etc.) usually don’t need parenthesesCorrect capitalization is required, not optionalConvention: Multiword variables use underscores, not camelCaseConvention: Standard indentation is two spacesNumbersNumbers may be written in decimal, hexadecimal, octal, or binaryDecimal: 3405691582Hex: 0xCAFEBABE or 0XCAFEBABEOctal: 031277535276 or 0o31277535276Binary: 0b11001010111111101011101010111110 or 0Betc.Numbers larger than four bytes are automatically treated as Bignum objectsFor readability, numbers may contain (but not begin or end with) underscoresExamples: 3_405_691_582, 0b_111_101_101Integers may be indexed to retrieve their bitsExample: 5.step(0, -1) { |i| print 6[i] }  000110Other data typesStrings can be singly quoted or doubly quotedDoubly quoted strings can interpolate values for #{expression} and for #@variable, and allow the usual escape charactersIn a singly quoted string, \' is the only recognized escape characterStrings are not immutableArrays are untyped and expandable, for example [1, 2, "hi"]Ruby has hashes: { :banana  'yellow', :cherry => 'red' }Ruby has regular expressions, dates, and timesRuby has ranges, such as 1..10Ruby has symbols, which stand for themselvesYou can think of them as immutable stringsExamples are :banana and :cherrySince they are immutable, they make good keys for hashesThey are also often used to refer to methods and variables: attr :do_itOperatorsAlmost all the Java operators, except ++ and --Ruby uses :: to mean the same as . in JavaRuby uses =~ and !~ for Perl-style regular expressions** is the exponentiation operator+ is used to concatenate stringsThere is a to_s method for converting other things to strings.. is an inclusive range, ... is an exclusive rangeSo 1..4 and 1...5 both mean 1, 2, 3, 40..k is a range object, but [0..k] is an array with one element, a rangedefined? is a prefix operator that returns a true value or nilRuby has both !, &&, || and also not, and, orPrecedence is ! > && > ||, but not > and == orAssignment and alias statementsAssignment statements use =, +=, *=, etc.You can have multiple assignment: x, y = y, x swaps the values of x and yRuby does not have the ++ and -- operatorsYou can create a new name for a method, operator, global variable, or regular expression backreferenceSyntax: alias new_name original_nameExample: alias display putsif statementsMulti-line version:if condition then codeelsif condition then codeelse codeendThe “then” is optional at the end of a lineSingle-line versions:if condition then code elsif condition then code else code end“then” is required to separate condition from codestatement if conditionunless statements“unless” means “if not”Multi-line version:unless condition then codeelse codeendThe “then” is optional at the end of a lineSingle-line versions:unless condition then code else code end“then” is required to separate condition from codestatement unless conditioncase and ===case expr_1 when expr_2 then code when expr_3, ..., expr_n then code else codeendCases do not fall through; no break is neededComparisons use expr_n === expr_1and not the other way aroundexpr_n === expr_1 has many meaningsSimple equality testexpr_1 =~ expr_nexpr_n.kindof? expr_1expr_n.include? expr_1The === operator has the misleading name “case equality operator”Loops in RubyRuby has several loopswhile condition do statementsendbegin statementsend while conditionuntil condition statementsendbegin statementsend until conditionfor variable in range do statementsendloop do statementsendstatement while conditionstatement until conditionloop { statements }However, loops are not used as often in Ruby as in other languagesInstead, Ruby programmers use iterator methodsLoop controlsbreak gets you out of loopsWithin a loop, next jumps to just before the loop testWithin a block, next exits the block with nilretry restarts the loop body after reevaluating the condition or getting the next iterated elementWithin a loop, redo restarts the loop body, but does not reevaluate the condition or get the next iterated elementWithin a block, redo restarts the yield or callExceptionsraise message raises a RuntimeErrorraise exception raises an error of the given typeraise exception, messagerescuerescue ExceptionType, ..., ExceptionTyperescue ExceptionType, ..., ExceptionType => variableCatches exceptions within a method, or within a begin...end block, or can be used as a


View Full Document

Penn CIT 597 - Ruby

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download Ruby
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Ruby and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Ruby 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?