Programcion en ruby

Solo disponible en BuenasTareas
  • Páginas : 61 (15139 palabras )
  • Descarga(s) : 15
  • Publicado : 7 de junio de 2010
Leer documento completo
Vista previa del texto
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 1 2

What'chu talkin' 'bout, Mister? 4
WhatIs Ruby Anyhow? 4 Installing Ruby 6 Windows 6 · Mac OS X 6 · Linux 7 Let's try her out! 8

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

Break it down now! 27
Methods 27 Defining Methods 28 · Using Methods 30 Blocks and Proc Objects 31 Block Basics 31 · Procs andBlocks 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 theEnvironment! 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 testall 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 itnecessary 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).

"I gotta go. There's a dude next to me and he's watching me type, which is sort of starting to creep me out. Yes dude next to me, I mean you."
Night-hen-gayle(bash.org)

0
What'chu talkin' 'bout, Mister?

Yes

there is a Chapter 0. There is a little bit of introductory stuff we need to talk about before we set you loose on Ruby. You wouldn't want to get psyched about a new gadget, get it home, and then figure out you need batteries, a grapefruit, and the ability to speak three languages to even open the box would you? You would? Well then answer methis: How would Ruby react to large, elastic monsters taking over an estuary? You don't know the answer!? Well, plainly we need to take a good, hard look at a few things before we turn you loose.

WHAT IS RUBY ANYHOW?
Ruby is an open-source, multi-paradigm, interpreted programming language (a bit of a mouthful I know! I'm going to explain it, I promise!). Ruby was created by Yukihiro "Matz"...
tracking img