Ruby
Notes for ProfessionalsRuby®
Notes for Professionals
200+ pages
of professional hints and tricks
GoalKicker.com
Free Programming Books
Disclaimer
This is an unocial free book created for educational purposes and is
not aliated with ocial Ruby® group(s) or company(s).
All trademarks and registered trademarks are
the property of their respective owners
Contents
About
...................................................................................................................................................................................
1
Chapter 1: Getting started with Ruby Language
..........................................................................................
2
Section 1.1: Hello World
Section 1.2: Hello World as a Self-Executable File—using Shebang (Unix-like operating systems only)
.................................................................................................................................................
2
..................................................................................................................................................................................
2
Section 1.3: Hello World from IRB
Section 1.4: Hello World without source files
Section 1.5: Hello World with tk
Section 1.6: My First Method
................................................................................................................................
3
..............................................................................................................
3
....................................................................................................................................
.........................................................................................................................................
Chapter 2: Casting (type conversion)
.................................................................................................................
6
Section 2.1: Casting to a Float
Section 2.2: Casting to a String
Section 2.3: Casting to an Integer
Section 2.4: Floats and Integers
......................................................................................................................................
....................................................................................................................................
................................................................................................................................
6
...................................................................................................................................
6
Chapter 3: Operators
..................................................................................................................................................
8
Section 3.1: Operator Precedence and Methods
Section 3.2: Case equality operator (===)
Section 3.3: Safe Navigation Operator
Section 3.4: Assignment Operators
Section 3.5: Comparison Operators
.................................................................................................................
......................................................................................................................
...........................................................................................................................
..........................................................................................................................
........................................................................................................
8
Chapter 4: Variable Scope and Visibility
.......................................................................................................
13
Section 4.1: Class Variables
Section 4.2: Local Variables
Section 4.3: Global Variables
Section 4.4: Instance Variables
........................................................................................................................................
.......................................................................................................................................
......................................................................................................................................
..................................................................................................................................
16
Chapter 5: Environment Variables
....................................................................................................................
18
Section 5.1: Sample to get user profile path
.............................................................................................................
18
Chapter 6: Constants
................................................................................................................................................
19
Section 6.1: Define a constant
Section 6.2: Modify a Constant
Section 6.3: Constants cannot be defined in methods
Section 6.4: Define and change constants in a class
....................................................................................................................................
..................................................................................................................................
............................................................................................
19
..............................................................................................
19
Chapter 7: Special Constants in Ruby
..............................................................................................................
20
Section 7.1: __FILE__
Section 7.2: __dir__
Section 7.3: $PROGRAM_NAME or $0
Section 7.4: $$
Section 7.5: $1, $2, etc
Section 7.6: ARGV or $*
Section 7.7: STDIN
Section 7.8: STDOUT
Section 7.9: STDERR
Section 7.10: $stderr
Section 7.11: $stdout
Section 7.12: $stdin
Section 7.13: ENV
..................................................................................................................................................
...................................................................................................................................................
.......................................................................................................................
20
..............................................................................................................................................................
..................................................................................................................................................
...............................................................................................................................................
.......................................................................................................................................................
...................................................................................................................................................
....................................................................................................................................................
....................................................................................................................................................
.....................................................................................................................................................
......................................................................................................................................................
..........................................................................................................................................................
3
4
6
6
10
11
11
Advertisement
12
13
14
15
19
19
20
20
20
20
20
20
20
20
21
21
21
21
Chapter 8: Comments
..............................................................................................................................................
22
Section 8.1: Single & Multiple line comments
............................................................................................................
22
Chapter 9: Arrays
.......................................................................................................................................................
23
........................................................................................................................................
...........................................................................................................................
................................................................................................................
.......................................................................................................................
................................................................................................................
................................................................................................................................
................................................................................
Section 9.1: Create Array of Strings
Section 9.2: Create Array with Array::new
Section 9.3: Create Array of Symbols
Section 9.4: Manipulating Array Elements
Section 9.5: Accessing elements
Section 9.6: Creating an Array with the literal constructor [ ]
Section 9.7: Decomposition
Section 9.8: Arrays union, intersection and dierence
Section 9.9: Remove all nil elements from an array with #compact
Section 9.10: Get all combinations / permutations of an array
Section 9.11: Inject, reduce
Section 9.12: Filtering arrays
Section 9.13: #map
Section 9.14: Arrays and the splat (*) operator
Section 9.15: Two-dimensional array
Section 9.16: Turn multi-dimensional array into a one-dimensional (flattened) array
Section 9.17: Get unique array elements
Section 9.18: Create Array of numbers
Section 9.19: Create an Array of consecutive numbers or letters
Section 9.20: Cast to Array from any object
...................................................................................................................
.....................................................................................................................
............................................................................................................
..........................................................................
..........................................................................................................................................
......................................................................................................................................
......................................................................................................................................................
............................................................................................
....................................................................
.............................................................................
........................................................................................................
........................................................................................................................
........................................
32
Chapter 10: Multidimensional Arrays
................................................................................................................
35
Section 10.1: Initializing a 2D array
Section 10.2: Initializing a 3D array
Section 10.3: Accessing a nested array
Section 10.4: Array flattening
.............................................................................................................................
............................................................................................................................
.....................................................................................................................
35
.....................................................................................................................................
35
Chapter 11: Strings
......................................................................................................................................................
37
..............................................
37
....................................................................................................................................
.................................................................................................................................
.............................................................................................................................
..................................................................................................................................
.....................................................................................................................................
.....................................................................................................................................
Section 11.1: Dierence between single-quoted and double-quoted String literals
Section 11.2: Creating a String
Section 11.3: Case manipulation
Section 11.4: String concatenation
Section 11.5: Positioning strings
Section 11.6: Splitting a String
Section 11.7: String starts with
Section 11.8: Joining Strings
Section 11.9: String interpolation
Section 11.10: String ends with
Section 11.11: Formatted strings
Section 11.12: String Substitution
Section 11.13: Multiline strings
Section 11.14: String character replacements
Section 11.15: Understanding the data in a string
................................................................................................................................
.....................................................................................................................................
..................................................................................................................................
.................................................................................................................................
......................................................................................................................................
............................................................................................................
.....................................................................................................
........................................................................................................................................
Chapter 12: DateTime
...............................................................................................................................................
44
Section 12.1: DateTime from string
Section 12.2: New
Section 12.3: Add/subtract days to DateTime
............................................................................................................................
.........................................................................................................................................................
..........................................................................................................
44
Chapter 13: Time
..........................................................................................................................................................
46
Section 13.1: How to use the strftime method
...........................................................................................................
Advertisement
46
23
23
24
24
25
26
26
27
28
28
29
30
30
31
31
32
32
33
33
35
35
37
38
38
39
40
40
40
41
41
41
41
41
42
43
44
44
Section 13.2: Creating time objects
............................................................................................................................
46
Chapter 14: Numbers
................................................................................................................................................
47
Section 14.1: Converting a String to Integer
Section 14.2: Creating an Integer
Section 14.3: Rounding Numbers
Section 14.4: Even and Odd Numbers
Section 14.5: Rational Numbers
Section 14.6: Complex Numbers
Section 14.7: Converting a number to a string
Section 14.8: Dividing two numbers
..............................................................................................................
...............................................................................................................................
...............................................................................................................................
.......................................................................................................................
.................................................................................................................................
.................................................................................................................................
.........................................................................................................
49
...........................................................................................................................
49
Chapter 15: Symbols
..................................................................................................................................................
50
Section 15.1: Creating a Symbol
Section 15.2: Converting a String to Symbol
Section 15.3: Converting a Symbol to String
.................................................................................................................................
50
.............................................................................................................
50
.............................................................................................................
51
Chapter 16: Comparable
.........................................................................................................................................
52
Section 16.1: Rectangle comparable by area
...........................................................................................................
52
Chapter 17: Control Flow
.........................................................................................................................................
53
...............................................................................................................................
.....................................................................................................................................
.......................................................................................................................
........................................................................................................................................
...............................................................................................................................................
..................................................................................................................................
......................................................................................................................................................
............................................................................................................................................
.................................................................................................................................
..............................................................................
57
Section 17.1: if, elsif, else and end
Section 17.2: Case statement
Section 17.3: Truthy and Falsy values
Section 17.4: Inline if/unless
Section 17.5: while, until
Section 17.6: Flip-Flop operator
Section 17.7: Or-Equals/Conditional assignment operator (||=)
Section 17.8: unless
Section 17.9: throw, catch
Section 17.10: Ternary operator
Section 17.11: Loop control with break, next, and redo
Section 17.12: return vs. next: non-local return in a block
Section 17.13: begin, end
Section 17.14: Control flow with logic statements
..............................................................................................................................................
.....................................................................................................
.............................................................................................
........................................................................................
Chapter 18: Methods
..................................................................................................................................................
63
Section 18.1: Defining a method
Section 18.2: Yielding to blocks
Section 18.3: Default parameters
Section 18.4: Optional parameter(s) (splat operator)
Section 18.5: Required default optional parameter mix
Section 18.6: Use a function as a block
Section 18.7: Single required parameter
Section 18.8: Tuple Arguments
Section 18.9: Capturing undeclared keyword arguments (double splat)
Section 18.10: Multiple required parameters
Section 18.11: Method Definitions are Expressions
.................................................................................................................................
..................................................................................................................................
...............................................................................................................................
.............................................................................................
Advertisement
..........................................................................................
.....................................................................................................................
...................................................................................................................
...................................................................................................................................
..............................................................
67
.............................................................................................................
....................................................................................................
Chapter 19: Hashes
.....................................................................................................................................................
69
Section 19.1: Creating a hash
Section 19.2: Setting Default Values
Section 19.3: Accessing Values
Section 19.4: Automatically creating a Deep Hash
Section 19.5: Iterating Over a Hash
Section 19.6: Filtering hashes
......................................................................................................................................
..........................................................................................................................
...................................................................................................................................
..................................................................................................
72
...........................................................................................................................
......................................................................................................................................
47
47
47
48
48
48
53
53
55
56
56
57
58
58
58
59
61
61
62
63
63
64
65
65
66
66
66
67
67
69
70
71
73
74
Section 19.7: Conversion to and from Arrays
Section 19.8: Overriding hash function
Section 19.9: Getting all keys or values of hash
Section 19.10: Modifying keys and values
Section 19.11: Set Operations on Hashes
...........................................................................................................
......................................................................................................................
.......................................................................................................
75
.................................................................................................................
...................................................................................................................
Chapter 20: Blocks and Procs and Lambdas
...............................................................................................
77
................................................................................................................................................
........................................................................................................
...........................................................................................
80
Section 20.1: Lambdas
Section 20.2: Partial Application and Currying
Section 20.3: Objects as block arguments to methods
Section 20.4: Converting to Proc
Section 20.5: Blocks
...............................................................................................................................
....................................................................................................................................................
.................................................................................................................................................
.........................................................................................................................................................
Chapter 21: Iteration
Section 21.1: Each
Section 21.2: Implementation in a class
Section 21.3: Iterating over complex objects
Section 21.4: For iterator
Section 21.5: Iteration with index
Section 21.6: Map
....................................................................................................................
............................................................................................................
84
.............................................................................................................................................
................................................................................................................................
.........................................................................................................................................................
Chapter 22: Exceptions
............................................................................................................................................
87
Section 22.1: Creating a custom exception type
Section 22.2: Handling multiple exceptions
Section 22.3: Handling an exception
Section 22.4: Raising an exception
Section 22.5: Adding information to (custom) exceptions
......................................................................................................
..............................................................................................................
.........................................................................................................................
............................................................................................................................
......................................................................................
90
Chapter 23: Enumerators
.......................................................................................................................................
91
Section 23.1: Custom enumerators
Section 23.2: Existing methods
Section 23.3: Rewinding
............................................................................................................................
91
...................................................................................................................................
..............................................................................................................................................
Chapter 24: Enumerable in Ruby
Section 24.1: Enumerable module
.......................................................................................................................
93
..............................................................................................................................
93
Chapter 25: Classes
...................................................................................................................................................
Advertisement
96
............................................................................................................................................
....................................................................................................................................
........................................................................................................................................
............................................................................................................................
Section 25.1: Constructor
Section 25.2: Creating a class
Section 25.3: Access Levels
Section 25.4: Class Methods types
Section 25.5: Accessing instance variables with getters and setters
Section 25.6: New, allocate, and initialize
Section 25.7: Dynamic class creation
Section 25.8: Class and instance variables
...............................................................................................................
.....................................................................................................................
............................................................................................................
..................................................................
100
Chapter 26: Inheritance
.........................................................................................................................................
104
Section 26.1: Subclasses
Section 26.2: What is inherited?
Section 26.3: Multiple Inheritance
Section 26.4: Mixins
Section 26.5: Refactoring existing classes to use Inheritance
............................................................................................................................................
...............................................................................................................................
............................................................................................................................
...................................................................................................................................................
..............................................................................
107
Chapter 27: method_missing
.............................................................................................................................
109
Section 27.1: Catching calls to an undefined method
Section 27.2: Use with block
Section 27.3: Use with parameter
.....................................................................................................................................
............................................................................................................................
............................................................................................
109
74
74
75
76
77
78
80
81
83
83
84
85
85
86
87
87
88
90
91
91
96
96
96
98
101
101
102
104
104
106
106
109
109
Section 27.4: Using the missing method
.................................................................................................................
110
Chapter 28: Regular Expressions and Regex Based Operations
.....................................................
111
...........................................................................................................................................
...........................................................................................................................................
111
........................................................................................
111
..........................................................................................................
Section 28.1: =~ operator
Section 28.2: Regular Expressions in Case Statements
Section 28.3: Groups, named and otherwise
Section 28.4: Quantifiers
Section 28.5: Common quick usage
Section 28.6: match? - Boolean Result
Section 28.7: Defining a Regexp
Section 28.8: Character classes
........................................................................................................................
...................................................................................................................
..............................................................................................................................
...............................................................................................................................
..............................................................................................................
......................................................................................................................
..........................................................................................................................
Chapter 29: File and I/O Operations
Section 29.1: Writing a string to a file
Section 29.2: Reading from STDIN
Section 29.3: Reading from arguments with ARGV
Section 29.4: Open and closing a file
Section 29.5: get a single char of input
Chapter 30: Ruby Access Modifiers
Section 30.1: Instance Variables and Class Variables
Section 30.2: Access Controls
...............................................................................................
116
......................................................................................................................
...................................................................................................................
.................................................................................................................
..................................................................................................................................
120
...........................................................................................
118
Chapter 31: Design Patterns and Idioms in Ruby
.....................................................................................
123
Section 31.1: Decorator Pattern
Section 31.2: Observer
Section 31.3: Singleton
Section 31.4: Proxy
...............................................................................................................................................
...............................................................................................................................................
........................................