Tải bản đầy đủ (.pdf) (147 trang)

Tài liệu Mr.Neighborly''''s Humble little ruby book doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.33 MB, 147 trang )



M R . N E I G H B O R L Y ' S
HUMBLE LITTLE
RUBY BOOK

M R . N E I G H B O R L Y ' S
HUMBLE LITTLE
RUBY BOOK
Jeremy McAnally
All content ©2006 Jeremy McAnally. All Right Reserved.
That means don't copy it.
For my wife, friends, and family,
thank you for the support and food.
Mostly the food.

TABLE OF CONTENTS
0
What'chu talkin' 'bout, Mister? 4
What Is Ruby Anyhow? 4
Installing Ruby 6
Windows 6 · Mac OS X 6 · Linux 7
Let's try her out! 8
1
Welcome to Ruby 10
Basic Concepts of Ruby 10
Types in Ruby 11
Strings 11 · Numbers 13
Collections 14
The Range 15 · The Array 16 · The Hash 20
Variables and the Like 23


2
Break it down now! 27
Methods 27
Defining Methods 28 · Using Methods 30
Blocks and Proc Objects 31
Block Basics 31 · Procs and Blocks 33 · Building Blocks 35
Your objects lack class! 36
Defining Classes 37 · Methods and Variables 38 · Attributes 40 ·
Access Control 41 ·
Class Scoped Objects 42
Modules 44
Creating Modules 44
Files 46
3
Hustle and flow (control) 48
Conditionals 48
The if statement 48 · The case Statement 51
Loops 53
Conditional Loops 53 · Iterating Loops and Blocks 54 · Statement
Modifiers 55 · Controlling Loops 56
Exceptions 58
Handling Exceptions 58 · Raising Exceptions 61 · My Own Exception
62 · Throw and Catch 62
4
The System Beneath 64
Filesystem Interaction 64
Writing to a file 66 · More file operations 67
Threads and Forks and Processes, Oh My! 68
Ruby thread basics 68 · Controlling threads 70 · Getting information
from threads 71 · Processes, the other way to do stuff 72

For the Environment! 73
Environment variables and the like 73 · The command line and you 73 ·
Ruby and its little corner of your computer 74
Win32 and Beyond 75
API 75 · The Registry 77 · OLE Automation 79
5
Looking Beyond Home 83
Networking and the Web 83
Socket Programming 83 · HTTP Networking 86 Other Network
Services 92 · Web Services 95
It's Like Distributed or Something 96
Data my base, please! 98
6
It's a Library! 101
String Manipulation 101
Instance Methods 101 · Regular Expressions 104
Date/Time 106
Dates 106 · Times 107 · Dates and Times 109
Hashing and Cryptography 109
Hashing 109 · Cryptography 110
Unit testing 111
Appendix A Links and the Like 116
Appendix B High Performance Ruby with C/C++ 118

A Note
In the following book, I will be using Ruby 1.8.5 to test all the Ruby
code. Each example can be copied and pasted directly into irb/fxri
and it should work famously. I have done so with each one to make
sure they run.
Each time I am showing output from irb, you will see a → character

followed by the output. Any method or variable name or
code/system related text is typeset in this font for easy discernment
from other text.
Any time I have found it necessary to differentiate a class object
from an instance object, I have erred on the side of standard
notation and went with the form of Class#Object (even though it's
very ugly and is not what the rest of the civilized world uses).

×