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
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 Bet you can t do this in Java Jan 13 2019 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 x end puts Enter two numbers STDOUT flush x Integer gets y Integer gets puts The GCD of x and y is euclid x y Enter two numbers 24 108 The GCD of 24 and 108 is 12 General 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 spaces Numbers Numbers may be written in decimal hexadecimal octal or binary Numbers larger than four bytes are automatically treated as Bignum objects For readability numbers may contain but not begin or end with underscores Decimal 3405691582 Hex 0xCAFEBABE or 0XCAFEBABE Octal 031277535276 or 0o31277535276 Binary 0b11001010111111101011101010111110 or 0Betc 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 000110 Other 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 it Operators 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 is an inclusive range is an exclusive range There is a to s method for converting other things to strings 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 or Assignment 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 puts if statements Multi line version if condition then code elsif condition then code else code end 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 condition unless statements unless means if not Multi line version unless condition then code else code end 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 condition case and case expr 1 when expr 2 then code when expr 3 expr n then code else code end Cases do not fall through no break is needed Comparisons use expr n expr 1 and 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 statements end begin statements end while condition until condition statements end begin statements end until condition for variable in range do statements end loop do statements end 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 methods Loop 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 call Exceptions 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 statement suffix Example on next slide Blocks A block is a set of statements enclosed by begin end by do end or by begin introduces a new scope variables seen for the first time inside a begin end block are local to that scope do end and are interchangeable Any block or method can include rescue or rescue ensure begin rescue ensure end is like Java s try catch finally Example begin code rescue exception code ensure code end Defining methods Defining an instance method Defining a class method def method name parameter1 parameterN statements end def Class name method name parameter1 parameterN statements end Parentheses are optional A method may contain explicit return and or return value statements A method may return multiple values return x 2 x x 2 A method which reaches the end returns the value of the last statement executed often nil A method may also have a block as an invisible parameter The block is not mentioned in the def line but is executed when the method evaluates the yield statement Defining classes class Employee Person means extends number 0 class variable attr reader name creates a getter for name def initialize name name name number 1 end end this is the constructor secretary Employee new William Calling methods Methods may be called as in Java Parentheses are usually unnecessary puts x x All values are objects and may have methods puts x x x y abs n abcdefg length A method may be passed a block as an invisible parameter def


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 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?