Ruby Programming
Total Page:16
File Type:pdf, Size:1020Kb
Ruby Programming Wikibooks.org December 1, 2012 On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipedia projects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. An URI to this license is given in the list of figures on page 249. If this document is a derived work from the contents of one of these projects and the content was still licensed by the project under this license at the time of derivation this document has to be licensed under the same, a similar or a compatible license, as stated in section 4b of the license. The list of contributors is included in chapter Contributors on page 243. The licenses GPL, LGPL and GFDL are included in chapter Licenses on page 253, since this book and/or parts of it may or may not be licensed under one or more of these licenses, and thus require inclusion of these licenses. The licenses of the figures are given in the list of figures on page 249. This PDF was generated by the LATEX typesetting software. The LATEX source code is included as an attachment (source.7z.txt) in this PDF file. To extract the source from the PDF file, we recommend the use of http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility or clicking the paper clip attachment symbol on the lower left of your PDF Viewer, selecting Save Attachment. After extracting it from the PDF file you have to rename it to source.7z. To uncompress the resulting archive we recommend the use of http://www.7-zip.org/. The LATEX source itself was generated by a program written by Dirk HÃijnniger, which is freely available under an open source license from http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf. This distribution also contains a configured version of the pdflatex compiler with all necessary packages and fonts needed to compile the LATEX source included in this PDF file. Contents 1 Overview . 3 1.1 Features . 3 1.2 References . 6 2 Installing Ruby . 7 2.1 Operating systems . 7 2.2 Building from Source . 10 2.3 Compile options . 10 2.4 Testing Installation . 10 2.5 References . 11 3 Ruby editors . 13 4 Notation conventions . 15 4.1 Command-line examples . 15 5 Interactive Ruby . 17 5.1 Running irb . 17 5.2 Understanding irb output . 18 6 Mailing List FAQ . 19 7 Basic Ruby - Hello world . 21 7.1 Hello world . 21 7.2 Comments . 22 7.3 Executable Ruby scripts . 22 8 Basic Ruby - Strings . 27 8.1 String literals . 27 8.2 Single quotes . 27 8.3 Double quotes . 28 8.4 puts . 29 8.5 print . 29 8.6 See also . 30 9 Basic Ruby - Alternate quotes . 31 9.1 Alternate single quotes . 31 9.2 Alternate double quotes . 32 10 Basic Ruby - Here documents . 33 10.1 Here documents . 33 10.2 Indenting . 34 10.3 Quoting rules . 35 11 Basic Ruby - Introduction to objects . 37 11.1 What is an object? . 37 11.2 Variables and objects . 37 11.3 Methods . 38 11.4 Reassigning a variable . 39 12 Basic Ruby - Ruby basics . 41 III Contents 13 Dealing with variables . 43 14 Program flow . 45 15 Writing functions . 47 16 Blocks . 49 17 Ruby is really, really object-oriented . 53 18 Basic Ruby - Data types . 55 18.1 Ruby Data Types . 55 18.2 Constants . 55 18.3 Symbols . 56 18.4 Hashes . 57 18.5 Arrays . 58 18.6 Strings . 62 18.7 Numbers (Integers and Floats) . 66 18.8 Additional String Methods . 67 19 Basic Ruby - Writing methods . 69 19.1 Defining Methods . 69 20 Basic Ruby - Classes and objects . 71 20.1 Ruby Classes . 71 20.2 Creating Instances of a Class . 71 20.3 Creating Classes . 71 20.4 Self . 72 20.5 Class Methods . 72 21 Basic Ruby - Exceptions . 75 22 Syntax - Lexicology . 77 22.1 Identifiers . 77 22.2 Comments . 77 22.3 Embedded Documentation . 77 22.4 Reserved Words . 78 22.5 Expressions . 78 23 Syntax - Variables and Constants . 79 23.1 Local Variables . 79 23.2 Instance Variables . 79 23.3 Class Variables . 80 23.4 Global Variables . 80 23.5 Constants . 80 23.6 Pseudo Variables . 81 23.7 Pre-defined Variables . 82 23.8 Pre-defined Constants . 85 23.9 Notes . 85 24 Syntax - Literals . 87 24.1 Numerics . 87 24.2 Strings . ..