E-Book, Englisch, 246 Seiten
Mandal Ruby Recipes
1. ed
ISBN: 978-1-4842-2469-4
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
A Problem-Solution Approach
E-Book, Englisch, 246 Seiten
ISBN: 978-1-4842-2469-4
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
Solve your Ruby programming problems that occur during your day-to-day scripting work. This book contains a varied selection of practical and interesting code recipes designed to make your coding life easier. Ruby Recipes includes solutions to problems in working with data, handling exceptions, writing blocks, and using regular expressions. This book provides ready scripts for both simple complex scripting tasks, which you can use readily or with only minor modifications. These scripts cover areas such as collections, classes and structures, functional programming, and log handling. With these handy recipes at your fingertips, you will be able to solve those niggling problems and become even more efficient.
What You Will Learn Install and run RubyRead and write data Write functionsWork with arrays, ranges, hashes, and iteratorsHandle dates and timeProcess XML as textMaster OOP concepts such as classes, objects, subclassing, and inheritanceConnect to databases
Who This Book Is For
Programmers new to Ruby, and web developers interested in knowing the foundations of the language.
A Master of Engineering (Mechanical Engineering) from IISc, Bangalore ('94), Malay is working in IT for the last 22 or so years. He worked across multiple geographical locations (India, Japan, Singapore, UK ..), across multiple technology stack and for quite a few well known organisations (Telstra, JP Morgan, Citigroup etc.). He quite often actively learns new things, and his creation (other than books) include Apps in Google Play, and some small wooden pieces of furniture at home.
Autoren/Hrsg.
Weitere Infos & Material
1;Contents at a Glance;5
2;Contents;6
3;About the Author;19
4;Acknowledgments;20
5;Introduction;21
6;Chapter 1: A Taste of Ruby;25
6.1; Small Task, Big Impact;25
6.2; Development Environment Installation;26
6.2.1; Windows;26
6.2.2; Mac;26
6.3; 1.1 The First Recipe: aka Hello World;27
6.3.1; Problem;28
6.3.2; Solution;28
6.3.3; How It Works;28
6.4; 1.2 Does It Cost Anything to Say Hello?;29
6.4.1; Problem;29
6.4.2; Solution;29
6.4.3; How It Works;30
6.5; 1.3 Sherlock Holmes and Learning;30
6.5.1; Problem;31
6.5.2; Solution;31
6.5.3; How It Works;31
6.6; 1.4 1 2 3 4, 1 2 3 4;32
6.6.1; Problem;32
6.6.2; Solution;32
6.6.3; How It Works;33
6.7; Interactive Ruby Shell;34
6.8; Summary;35
6.9; Exercises;35
6.9.1; Exercise 1.1;35
6.9.2; Exercise 1.2;36
6.9.3; Exercise 1.3;36
7;Chapter 2: Working with Files and Strings;37
7.1; Manipulating Strings;37
7.1.1; length or size;37
7.1.2; empty?;37
7.1.3; strip;38
7.1.4;Outline Placeholder;38
7.1.5;Outline Placeholder;38
7.1.6; capitalize;38
7.1.7; downcase and upcase;38
7.1.8; chars;39
7.1.9; index;39
7.1.10; insert;39
7.1.11; delete;39
7.1.12; include?;40
7.1.13; slice;40
7.1.14; count;40
7.1.15; partition;40
7.1.16; tr;40
7.1.17; reverse;41
7.1.18; sub (and gsub);41
7.1.19; scan;41
7.1.20; split;42
7.2; String Formatting;43
7.3; 2.1 Accepting Input from the Console;43
7.3.1; Problem;43
7.3.2; Solution;43
7.3.3; How It Works;44
7.4; 2.2 Accepting Numbers as Input;45
7.4.1; Problem;45
7.4.2; Solution;45
7.4.3; How It Works;45
7.5; 2.3 Handling the Newline;46
7.5.1; Problem;46
7.5.2; Solution;46
7.5.3; More on Getting Rid of the Newline;47
7.6; 2.4 Formatting Strings;49
7.6.1; Problem;49
7.6.2; Solution;50
7.6.3; How It Works;50
7.7; 2.5 Processing Command-Line Arguments;51
7.7.1;Problem;51
7.7.2; Solution;51
7.7.3; How It Works;51
7.8; 2.6 Reading from a File;52
7.8.1; Problem;52
7.8.2; Solution;52
7.8.3; How It Works;53
7.9; 2.7 Writing to a File;53
7.9.1; Problem;53
7.9.2; Solution;53
7.9.3; How It Works;54
7.10; 2.8 Getting Started with Exception Handling;54
7.10.1; Problem;54
7.10.2; Solution;55
7.10.2.1; Initial Execution Context;55
7.11; 2.9 Importing Code;56
7.11.1; Problem;56
7.11.2; Solution;57
7.12; 2.10 Creating and Deleting Directories;57
7.12.1; Problem;57
7.12.2; Solution;57
7.12.3; How It Works;58
7.13; 2.11 Creating a Whole Directory Path;58
7.13.1; Problem;58
7.13.2; Solution;58
7.13.3; How It Works;58
7.14; 2.12 Reading Multiple Lines from a File;59
7.14.1; Problem;59
7.14.2; Solution;60
7.14.3; How It Works;60
7.15; 2.13 Reading a File in One Shot;62
7.15.1; Problem;62
7.15.2; Solution;63
7.15.3; How It Works;63
7.16; 2.14 Working with Strings;63
7.16.1; Problem;63
7.16.2; Solution;63
7.16.3; How It Works;64
7.16.3.1; Concatenation;65
7.16.3.2; Expression Evaluation;65
7.17; 2.15 Converting Numbers to a String;65
7.17.1; Problem;65
7.17.2; Solution;65
7.18; 2.16 Extracting Information from Strings;66
7.18.1; Problem;66
7.18.2; Task: Change the Order of Names;66
7.18.3; Solution;67
7.18.4; How It Works;67
7.18.5; Task: Totaling the Shopping List;67
7.18.6; Solution;67
7.19; Exercises;68
7.19.1; Exercise 2.1;68
7.19.2; Exercise 2.2;68
8;Chapter 3: Language Elements;69
8.1; Commenting on Commenting;69
8.2; Variables, Operators …;69
8.2.1; Working with Numbers;70
8.2.2; Logical and Other Operators;72
8.2.2.1; Reference and Value Equality;72
8.2.2.2; Checking If an Object Is nil;72
8.2.2.3; General Comparison Operator;73
8.2.2.4; Assignment Operators;73
8.2.2.5; Mass Assignment;74
8.2.2.6; Bitwise, Logical, and Ternary Operators;74
8.2.2.7; The Range Operators;75
8.2.2.8; The dot (.) Operator;75
8.2.2.9; Some Other Operators;76
8.2.3; Pattern Matching Operators;76
8.2.4; Using Ranges;76
8.2.4.1; Interval;77
8.2.4.2; Sequences;77
8.2.4.3; Conditions;78
8.3; Conditional Constructs/Control Flow;79
8.3.1; if;79
8.3.2; nil check;81
8.3.3; unless;82
8.3.4; Ternary operator;82
8.3.5; case;82
8.3.6; while;84
8.3.7; break, redo, next;85
8.3.8; until;86
8.3.9; for;87
8.4; 3.1 Handling Exceptions;87
8.4.1; Problem;87
8.4.2; Solution;87
8.4.2.1; Single Line Rescue;88
8.4.2.2; catch and throw;89
8.5; 3.2 Working with Predefined Variables and Constants;89
8.5.1; Problem;89
8.5.2; Solution;90
8.5.3; Predefined Constants;92
8.6; 3.3 Running OS Commands;92
8.6.1; Problem;92
8.6.2; Solution;92
8.7; 3.4 Initializing and Finalizing Code;92
8.7.1; Problem;92
8.7.2; Solution;93
8.8; 3.5 Defining Functions;93
8.8.1; Problem;93
8.8.2; Solution;93
8.8.2.1; Functions with Arguments;94
8.8.2.2; Function Arguments with Default Values;94
8.8.2.3; Functions with a Variable Number of Arguments;95
8.8.2.4; Return Value from Functions;95
8.8.2.5; Recurrence;96
8.9; Exercises;96
8.9.1; Exercise 3.1;96
8.9.2; Exercise 3.2;97
8.9.3; Exercise 3.3;97
9;Chapter 4: Collections;98
9.1; 4.1 Creating and Initializing Arrays;99
9.1.1; Problem;99
9.1.2; Solution;99
9.2; 4.2 Accessing Array Elements;101
9.2.1; Problem;101
9.2.2; Solution;101
9.3; 4.3 Inserting an Element at a Certain Position;102
9.3.1; Problem;102
9.3.2; Solution;102
9.4; 4.4 Working with Multidimensional Arrays;103
9.4.1; Problem;103
9.4.2; Solution;103
9.5; 4.5 Working with Arrays;104
9.5.1; Problem;104
9.5.2; Solution;104
9.5.2.1; empty?;104
9.5.2.2; fill;104
9.5.2.3; Add, Subtract, Compare, and Contrast;105
9.5.2.3.1;Outline Placeholder;105
9.5.2.3.2; concat;105
9.5.2.3.3;Outline Placeholder;105
9.5.2.3.4;Outline Placeholder;105
9.5.2.3.5;Outline Placeholder;105
9.5.2.3.6;Outline Placeholder;106
9.5.2.3.7;Outline Placeholder;106
9.5.2.4; Set Operations;106
9.5.2.4.1; | (or union);106
9.5.2.4.2; & (or intersection);106
9.5.2.4.3; uniq;107
9.5.2.5; In Place Operations;107
9.5.2.6; sort (and sort!);107
9.5.2.7; reverse (and reverse!);108
9.5.2.8; Further Access and Manipulation;108
9.5.2.8.1; include?;108
9.5.2.8.2; index;108
9.5.2.8.3; rindex;108
9.5.2.8.4; values_at;109
9.5.2.8.5; fetch;109
9.5.2.8.6; insert;109
9.5.2.8.7; delete;110
9.5.2.8.8; delete_at;110
9.5.2.8.9; join;111
9.5.2.8.10; compact;111
9.5.2.8.11; clear;111
9.6; 4.6 Creating Hashes;111
9.6.1; Problem;111
9.6.2; Solution;111
9.7; 4.7 Adding New Elements to a Hash;112
9.7.1; Problem;112
9.7.2; Solution;112
9.8; 4.8 Working with Hashes;113
9.8.1; Problem;113
9.8.2; Solution;114
9.8.2.1; clear;114
9.8.2.2; empty?;114
9.8.2.3; has_key?;114
9.8.2.4; has_value?;114
9.8.2.5; key;115
9.8.2.6; fetch;115
9.8.2.7; values_at;115
9.8.2.8; delete;116
9.8.2.9; invert;116
9.8.2.10; to_a;116
9.8.2.11;Outline Placeholder;116
9.8.2.12; merge (and merge!);117
9.9; 4.9 Creating a Collection of Unique Objects;117
9.9.1; Problem;117
9.9.2; Solution;117
9.10; 4.10 Inspecting a Set;118
9.10.1; Problem;118
9.10.2; Solution;118
9.11; 4.11 Working with Sets;119
9.11.1; Problem;119
9.11.2; Solution;119
9.11.2.1; Checking and Changing;119
9.11.2.1.1; length (or size);119
9.11.2.1.2; empty?;119
9.11.2.1.3; include?;119
9.11.2.1.4; clear;120
9.11.2.1.5; << (or add);120
9.11.2.1.6; merge;120
9.11.2.1.7; delete;120
9.11.2.1.8; subtract;120
9.11.2.1.9;Outline Placeholder;121
9.11.2.2; Set Operations;121
9.11.2.2.1; + (or | or union);121
9.11.2.2.2; & (or intersection);121
9.11.2.2.3; intersect?;122
9.11.2.2.4; disjoint?;122
9.11.2.2.5; - (or difference);122
9.11.2.2.6;Outline Placeholder;122
9.11.2.2.7; Subset and superset;122
9.11.2.3; Flattening and Conversion;123
9.12; Exercises;124
9.12.1; Exercise 4.1;124
9.12.2; Exercise 4.2;124
10;Chapter 5: Blocks and Iterators;125
10.1; 5.1 Associating Blocks with Functions;126
10.1.1; Problem;126
10.1.2; Solution;126
10.2; 5.2 Adding Arguments to a Block;128
10.2.1; Problem;128
10.2.2; Solution;128
10.3; 5.3 Initializing and Finalizing Code;130
10.3.1; Problem;130
10.3.2; Solution;130
10.4; 5.4 Iterating over Data;131
10.4.1; Problem;131
10.4.2; Solution;131
10.4.2.1; each;131
10.4.2.2; step;134
10.4.2.3; select and reject;134
10.4.2.3.1; For Arrays;135
10.4.2.3.2; For Hashes;135
10.4.2.3.3; For Sets;136
10.4.2.4; map or collect;136
10.4.2.4.1; For Arrays;137
10.4.2.4.2; For Sets;137
10.4.2.4.3; For Hashes;137
10.4.2.5; delete_if and keep_if;138
10.4.2.5.1; For Arrays;138
10.4.2.5.2; For Sets;138
10.4.2.5.3; For Hashes;138
10.4.2.6; sort;139
10.5; Exercises;139
10.5.1; Exercise 5.1;139
10.5.2; Exercise 5.2;140
10.5.3; Exercise 5.3;140
11;Chapter 6: Input-Output;141
11.1; 6.1 Querying a CSV File;141
11.1.1; Problem;141
11.1.2; Solution;142
11.1.2.1; Subtask 1: A Person’s Birthday;142
11.1.2.1.1; Writing the Code;142
11.1.2.1.2; Testing;144
11.1.2.2; Subtask 2: ( The Names of) the Youngest and the Oldest Persons;146
11.1.2.3; Date Handling by API;149
11.1.2.4; Subtask 3: Persons with a Birthday in a Given Month;150
11.2; 6.2 Sorting Text;151
11.2.1; Problem;151
11.2.2; Solution;152
11.2.3; How It Works;153
11.3; 6.3 Checking User Input;153
11.3.1; Problem;153
11.3.2; Solution;153
11.4; 6.4 Storing Data in a Structured Manner;158
11.4.1; Problem;158
11.4.2; Solution;158
11.5; 6.5 Working with Directories;160
11.5.1; Problem;160
11.5.2; Solution;160
11.5.2.1; mkdir;161
11.5.2.2; rmdir;161
11.5.2.3; pwd;161
11.5.2.4; chdir;162
11.5.2.5; home;162
11.5.2.6; exist?;162
11.5.2.7; entries;163
11.5.2.8; new;163
11.5.2.9; each;163
11.5.2.10; foreach;164
11.5.2.11; glob;164
11.6; 6.6 Dividing Files into Subdirectories;166
11.6.1; Problem;166
11.6.2; Solution;166
11.7; 6.7 Adding Text to Files Using a Batch Operation;168
11.7.1; Problem;168
11.7.2; Solution;168
11.7.2.1; Approach 1: Output From Command Execution;169
11.7.2.2; Approach 2: Use Dir.glob;170
11.7.2.3; Adding the Comments in Each File;170
12;Chapter 7: Regular Expressions;172
12.1; 7.1 Searching Within a File;172
12.1.1; Problem;172
12.1.2; Solution;174
12.1.3; How It Works;174
12.2; 7.2 Finding Only the Matched String;176
12.2.1; Problem;176
12.2.2; Solution;176
12.2.3;How It Works;176
12.3; 7.3 Working with Character Classes;178
12.3.1; Problem;178
12.3.2; Solution;178
12.3.3; How It Works;178
12.3.3.1; Negation;179
12.3.3.2; Range;180
12.3.3.3; Union;180
12.3.3.4; Intersection;181
12.3.3.5; Intersection with Negation;181
12.3.3.6; Common Character Classes;182
12.3.3.7; Predefined Character Classes;182
12.3.3.7.1; Any Single Character: dot;183
12.3.3.7.2; Whitespace and Non-Whitespace;183
12.3.3.8; Special Characters;184
12.3.3.9; Escape Sequence;185
12.4; 7.4 Finding Significant Positions in a String;185
12.4.1; Problem;185
12.4.2; Solution;185
12.4.2.1; End of a Source String;186
12.4.2.2; Word Boundary and Non-Word Boundary;187
12.4.2.3; Start and End of a String;188
12.4.2.4; Interaction of Subpatterns;189
12.4.2.5; Looking for Multiple Groups;191
12.5; 7.5 Using Non-Capturing Groups;192
12.5.1; Problem;192
12.5.2; Solution;192
12.6; 7.6 Understanding the Regex Engine and Backtracking;193
12.6.1; Problem;193
12.6.2; Solution;193
12.6.3; Plane Forward Search;194
12.6.4; Backtracking;194
12.6.5; More on Greedy (Meta-) Characters;195
12.7; 7.7 Finding Repeated Patterns;196
12.7.1; Problem;196
12.7.2; Solution;196
12.7.3; How It Works;197
12.7.3.1; Octal Codes and Backreferences;198
12.7.3.2;Named Backreferences;199
12.8; 7.8 Finding a Match and Excluding Some of It in the Result;199
12.8.1; Problem;199
12.8.2; Solution;200
12.8.2.1; Negative Lookahead Assertion;201
12.8.2.2; Lookbehind Assertion;201
12.8.2.3; Negative Lookbehind Assertion;202
12.9; 7.9 Inserting Comments in a Regular Expression;202
12.9.1; Problem;202
12.9.2; Solution;202
12.10; 7.10 Modifying Results;203
12.10.1; Problem;203
12.10.2; Solution;203
12.10.3; How It Works;204
12.11; 7.11 Using Non-Backtracking Groups;205
12.11.1; Problem;205
12.11.2; Solution;205
12.11.3; How It Works;205
12.12; 7.12 Replacing Substrings Using Regular Expressions;206
12.12.1; Problem;206
12.12.2; Solution;206
12.13; 7.13 Using the scan Function with Regular Expressions;207
12.14; Exercises;208
12.14.1; Exercise 7.1;208
12.14.2; Exercise 7.2;209
13;Chapter 8: Putting It into Action;210
13.1; 8.1 Removing Block CommentedCode;210
13.1.1; Problem;210
13.1.2; Solution;210
13.2; 8.2 Searching and Replacing in Text Files;216
13.2.1; Problem;216
13.2.2; Solution;216
13.3; 8.3 Removing Duplicates from a Text File;221
13.3.1; Problem;221
13.3.2; Solution;221
13.3.2.1; The Technical Problem Specification;223
13.3.2.2; Step 1: Remove the Comments;223
13.3.2.3; Step 2: Remove Optional Keywords;224
13.3.2.4; Step 3: Collect Tokens;227
13.3.2.5; Step 4: Get Distinct Values;231
13.4; 8.4 Reading XML as Text;232
13.4.1; Problem;232
13.4.2; Solution;232
13.5; 8.5 A Case for Hash Buckets;234
13.5.1; Problem;234
13.5.2; Solution;235
13.5.3; How It Works;236
14;Appendix A: Solutions to Exercises;237
14.1;Solutions for Chapter 1;237
14.1.1;(Solution) Excercise 1.1;237
14.1.2;(Solution) Excercise 1.2;237
14.1.3;(Solution) Excercise 1.3;237
14.2;Solutions for Chapter 2;238
14.2.1;(Solution) Excercise 2.1;238
14.2.2;(Solution) Excercise 2.2;238
14.3;Solutions for Chapter 3;238
14.3.1;(Solution) Excercise 3.1;238
14.3.2;(Solution) Excercise 3.2;239
14.3.3;(Solution) Excercise 3.3;239
14.4;Solutions for Chapter 4;239
14.4.1;(Solution) Excercise 4.1;239
14.4.2;(Solution) Excercise 4.2;240
14.5;Solutions for Chapter 5;241
14.5.1;(Solution) Excercise 5.1;241
14.5.2;(Solution) Excercise 5.2;241
14.5.3;(Solution) Excercise 5.3;241
14.6;Solutions for Chapter 7;242
14.6.1;(Solution) Excercise 7.1;242
14.6.2;(Solution) Excercise 7.2;242
15;Index;243




