Ruby Recipes: A Problem-Solution Approach

Ruby Recipes: A Problem-Solution Approach

Ruby Recipes: A Problem-Solution Approach
Автор: Mandal Malay
Дата выхода: 2016
Издательство: Apress Media, LLC.
Количество страниц: 246
Размер файла: 1007.0 KB
Тип файла: PDF
Добавил: codelibs
 Проверить на вирусы

 Contents at a Glance....5

Contents....6

About the Author....19

Acknowledgments....20

Introduction....21

Chapter 1: A Taste of Ruby....25

Small Task, Big Impact....25

Development Environment Installation....26

Windows....26

Mac....26

1.1 The First Recipe: aka Hello World....27

Problem....28

Solution....28

How It Works....28

1.2 Does It Cost Anything to Say Hello?....29

Problem....29

Solution....29

How It Works....30

1.3 Sherlock Holmes and Learning....30

Problem....31

Solution....31

How It Works....31

1.4 1 2 3 4, 1 2 3 4....32

Problem....32

Solution....32

How It Works....33

Interactive Ruby Shell....34

Summary....35

Exercises....35

Exercise 1.1....35

Exercise 1.2....36

Exercise 1.3....36

Chapter 2: Working with Files and Strings....37

Manipulating Strings....37

length or size....37

empty?....37

strip....38

Outline Placeholder....38

Outline Placeholder....38

capitalize....38

downcase and upcase....38

chars....39

index....39

insert....39

delete....39

include?....40

slice....40

count....40

partition....40

tr....40

reverse....41

sub (and gsub)....41

scan....41

split....42

String Formatting....43

2.1 Accepting Input from the Console....43

Problem....43

Solution....43

How It Works....44

2.2 Accepting Numbers as Input....45

Problem....45

Solution....45

How It Works....45

2.3 Handling the Newline....46

Problem....46

Solution....46

More on Getting Rid of the Newline....47

2.4 Formatting Strings....49

Problem....49

Solution....50

How It Works....50

2.5 Processing Command-Line Arguments....51

Problem....51

Solution....51

How It Works....51

2.6 Reading from a File....52

Problem....52

Solution....52

How It Works....53

2.7 Writing to a File....53

Problem....53

Solution....53

How It Works....54

2.8 Getting Started with Exception Handling....54

Problem....54

Solution....55

Initial Execution Context....55

2.9 Importing Code....56

Problem....56

Solution....57

2.10 Creating and Deleting Directories....57

Problem....57

Solution....57

How It Works....58

2.11 Creating a Whole Directory Path....58

Problem....58

Solution....58

How It Works....58

2.12 Reading Multiple Lines from a File....59

Problem....59

Solution....60

How It Works....60

2.13 Reading a File in One Shot....62

Problem....62

Solution....63

How It Works....63

2.14 Working with Strings....63

Problem....63

Solution....63

How It Works....64

Concatenation....65

Expression Evaluation....65

2.15 Converting Numbers to a String....65

Problem....65

Solution....65

2.16 Extracting Information from Strings....66

Problem....66

Task: Change the Order of Names....66

Solution....67

How It Works....67

Task: Totaling the Shopping List....67

Solution....67

Exercises....68

Exercise 2.1....68

Exercise 2.2....68

Chapter 3: Language Elements....69

Commenting on Commenting....69

Variables, Operators …....69

Working with Numbers....70

Logical and Other Operators....72

Reference and Value Equality....72

Checking If an Object Is nil....72

General Comparison Operator....73

Assignment Operators....73

Mass Assignment....74

Bitwise, Logical, and Ternary Operators....74

The Range Operators....75

The dot (.) Operator....75

Some Other Operators....76

Pattern Matching Operators....76

Using Ranges....76

Interval....77

Sequences....77

Conditions....78

Conditional Constructs/Control Flow....79

if....79

nil check....81

unless....82

Ternary operator....82

case....82

while....84

break, redo, next....85

until....86

for....87

3.1 Handling Exceptions....87

Problem....87

Solution....87

Single Line Rescue....88

catch and throw....89

3.2 Working with Predefined Variables and Constants....89

Problem....89

Solution....90

Predefined Constants....92

3.3 Running OS Commands....92

Problem....92

Solution....92

3.4 Initializing and Finalizing Code....92

Problem....92

Solution....93

3.5 Defining Functions....93

Problem....93

Solution....93

Functions with Arguments....94

Function Arguments with Default Values....94

Functions with a Variable Number of Arguments....95

Return Value from Functions....95

Recurrence....96

Exercises....96

Exercise 3.1....96

Exercise 3.2....97

Exercise 3.3....97

Chapter 4: Collections....98

4.1 Creating and Initializing Arrays....99

Problem....99

Solution....99

4.2 Accessing Array Elements....101

Problem....101

Solution....101

4.3 Inserting an Element at a Certain Position....102

Problem....102

Solution....102

4.4 Working with Multidimensional Arrays....103

Problem....103

Solution....103

4.5 Working with Arrays....104

Problem....104

Solution....104

empty?....104

fill....104

Add, Subtract, Compare, and Contrast....105

Outline Placeholder....105

concat....105

Outline Placeholder....105

Outline Placeholder....105

Outline Placeholder....105

Outline Placeholder....106

Outline Placeholder....106

Set Operations....106

| (or union)....106

& (or intersection)....106

uniq....107

In Place Operations....107

sort (and sort!)....107

reverse (and reverse!)....108

Further Access and Manipulation....108

include?....108

index....108

rindex....108

values_at....109

fetch....109

insert....109

delete....110

delete_at....110

join....111

compact....111

clear....111

4.6 Creating Hashes....111

Problem....111

Solution....111

4.7 Adding New Elements to a Hash....112

Problem....112

Solution....112

4.8 Working with Hashes....113

Problem....113

Solution....114

clear....114

empty?....114

has_key?....114

has_value?....114

key....115

fetch....115

values_at....115

delete....116

invert....116

to_a....116

Outline Placeholder....116

merge (and merge!)....117

4.9 Creating a Collection of Unique Objects....117

Problem....117

Solution....117

4.10 Inspecting a Set....118

Problem....118

Solution....118

4.11 Working with Sets....119

Problem....119

Solution....119

Checking and Changing....119

length (or size)....119

empty?....119

include?....119

clear....120

<< (or add)....120

merge....120

delete....120

subtract....120

Outline Placeholder....121

Set Operations....121

+ (or | or union)....121

& (or intersection)....121

intersect?....122

disjoint?....122

- (or difference)....122

Outline Placeholder....122

Subset and superset....122

Flattening and Conversion....123

Exercises....124

Exercise 4.1....124

Exercise 4.2....124

Chapter 5: Blocks and Iterators....125

5.1 Associating Blocks with Functions....126

Problem....126

Solution....126

5.2 Adding Arguments to a Block....128

Problem....128

Solution....128

5.3 Initializing and Finalizing Code....130

Problem....130

Solution....130

5.4 Iterating over Data....131

Problem....131

Solution....131

each....131

step....134

select and reject....134

For Arrays....135

For Hashes....135

For Sets....136

map or collect....136

For Arrays....137

For Sets....137

For Hashes....137

delete_if and keep_if....138

For Arrays....138

For Sets....138

For Hashes....138

sort....139

Exercises....139

Exercise 5.1....139

Exercise 5.2....140

Exercise 5.3....140

Chapter 6: Input-Output....141

6.1 Querying a CSV File....141

Problem....141

Solution....142

Subtask 1: A Person’s Birthday....142

Writing the Code....142

Testing....144

Subtask 2: ( The Names of) the Youngest and the Oldest Persons....146

Date Handling by API....149

Subtask 3: Persons with a Birthday in a Given Month....150

6.2 Sorting Text....151

Problem....151

Solution....152

How It Works....153

6.3 Checking User Input....153

Problem....153

Solution....153

6.4 Storing Data in a Structured Manner....158

Problem....158

Solution....158

6.5 Working with Directories....160

Problem....160

Solution....160

mkdir....161

rmdir....161

pwd....161

chdir....162

home....162

exist?....162

entries....163

new....163

each....163

foreach....164

glob....164

6.6 Dividing Files into Subdirectories....166

Problem....166

Solution....166

6.7 Adding Text to Files Using a Batch Operation....168

Problem....168

Solution....168

Approach 1: Output From Command Execution....169

Approach 2: Use Dir.glob....170

Adding the Comments in Each File....170

Chapter 7: Regular Expressions....172

7.1 Searching Within a File....172

Problem....172

Solution....174

How It Works....174

7.2 Finding Only the Matched String....176

Problem....176

Solution....176

How It Works....176

7.3 Working with Character Classes....178

Problem....178

Solution....178

How It Works....178

Negation....179

Range....180

Union....180

Intersection....181

Intersection with Negation....181

Common Character Classes....182

Predefined Character Classes....182

Any Single Character: dot....183

Whitespace and Non-Whitespace....183

Special Characters....184

Escape Sequence....185

7.4 Finding Significant Positions in a String....185

Problem....185

Solution....185

End of a Source String....186

Word Boundary and Non-Word Boundary....187

Start and End of a String....188

Interaction of Subpatterns....189

Looking for Multiple Groups....191

7.5 Using Non-Capturing Groups....192

Problem....192

Solution....192

7.6 Understanding the Regex Engine and Backtracking....193

Problem....193

Solution....193

Plane Forward Search....194

Backtracking....194

More on Greedy (Meta-) Characters....195

7.7 Finding Repeated Patterns....196

Problem....196

Solution....196

How It Works....197

Octal Codes and Backreferences....198

Named Backreferences....199

7.8 Finding a Match and Excluding Some of It in the Result....199

Problem....199

Solution....200

Negative Lookahead Assertion....201

Lookbehind Assertion....201

Negative Lookbehind Assertion....202

7.9 Inserting Comments in a Regular Expression....202

Problem....202

Solution....202

7.10 Modifying Results....203

Problem....203

Solution....203

How It Works....204

7.11 Using Non-Backtracking Groups....205

Problem....205

Solution....205

How It Works....205

7.12 Replacing Substrings Using Regular Expressions....206

Problem....206

Solution....206

7.13 Using the scan Function with Regular Expressions....207

Exercises....208

Exercise 7.1....208

Exercise 7.2....209

Chapter 8: Putting It into Action....210

8.1 Removing Block CommentedCode....210

Problem....210

Solution....210

8.2 Searching and Replacing in Text Files....216

Problem....216

Solution....216

8.3 Removing Duplicates from a Text File....221

Problem....221

Solution....221

The Technical Problem Specification....223

Step 1: Remove the Comments....223

Step 2: Remove Optional Keywords....224

Step 3: Collect Tokens....227

Step 4: Get Distinct Values....231

8.4 Reading XML as Text....232

Problem....232

Solution....232

8.5 A Case for Hash Buckets....234

Problem....234

Solution....235

How It Works....236

Appendix A: Solutions to Exercises....237

Solutions for Chapter 1....237

(Solution) Excercise 1.1....237

(Solution) Excercise 1.2....237

(Solution) Excercise 1.3....237

Solutions for Chapter 2....238

(Solution) Excercise 2.1....238

(Solution) Excercise 2.2....238

Solutions for Chapter 3....238

(Solution) Excercise 3.1....238

(Solution) Excercise 3.2....239

(Solution) Excercise 3.3....239

Solutions for Chapter 4....239

(Solution) Excercise 4.1....239

(Solution) Excercise 4.2....240

Solutions for Chapter 5....241

(Solution) Excercise 5.1....241

(Solution) Excercise 5.2....241

(Solution) Excercise 5.3....241

Solutions for Chapter 7....242

(Solution) Excercise 7.1....242

(Solution) Excercise 7.2....242

Index....243

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 Ruby
  • Read and write data
  • Write functions
  • Work with arrays, ranges, hashes, and iterators
  • Handle dates and time
  • Process XML as text
  • Master OOP concepts such as classes, objects, subclassing, and inheritance
  • Connect to databases

Who This Book Is For

Programmers new to Ruby, and web developers interested in knowing the foundations of the language.


Похожее:

Список отзывов:

Нет отзывов к книге.