Programming Ruby 1.9 & 2.0: The Pragmatic Programmers’ Guide

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

This book is the only complete reference for both Ruby 1.9 and Ruby 2.0, the very latest version of Ruby.

2013 marks the 20th anniversary of the Ruby language. We're proud that throughout its history, we've continued to cover the latest version of Ruby.

Would you like to go from first idea to working code much, much faster? Do you currently spend more time satisfying the compiler instead of your clients or end users? Are you frustrated with demanding languages that seem to get in your way, instead of getting the work done? Are you using Rails, and want to dig deeper into the underlying Ruby language? If so, then we've got a language and book for you!

Ruby is a fully object-oriented language, much like the classic object-oriented language, Smalltalk. Like Smalltalk, it is dynamically typed (as opposed to Java or C++), but unlike Smalltalk, Ruby features the same conveniences found in modern scripting languages such as Perl and Python.

The combination of the power of a pure object-oriented language with the convenience of a scripting language makes Ruby a favorite tool of intelligent, forward-thinking programmers.

The Pickaxe contains four major sections:

An acclaimed tutorial on using Ruby. The definitive reference to the language. Complete documentation of all built-in classes, modules, and methods. Complete descriptions of all 97 standard libraries.

This is the reference manual for Ruby, including a description of all the standard library modules, a complete reference to all built-in classes and modules (including all the new and changed methods introduced by Ruby 1.9, 1.9.2, 1.9.3, and 2.0). It also includes all the new and changed syntax and semantics introduced since Ruby 1.8. Learn about the new parameter passing rules, local variable scoping in blocks, fibers, and the new block declaration syntax, among other exciting new features.

About Ruby 2.0

Ruby 2.0 is a minor update to Ruby 1.9, unlike the more major updates from Ruby 1.8 to Ruby 1.9.

The major language changes in Ruby 2.0 are the addition of keyword arguments and the change to use UTF-8 as the default source file encoding.

There are a number of additions to the standard library, including:

@Enumerator::Lazy@, which adds support for lazy access to potentially infinite lists. Refinements allow you to encapsulate changes to third-party classes, and scope their application to individual source files, preventing your changes from polluting the global application.

You'll also find that Ruby 2 is faster, and has memory management improvements that make it more server-friendly.

All told, there are over 110 sections of the book that have been flagged and cross-linked to indicate 2.0 content.

What You Need

This book assumes you have a basic understanding of object-oriented programming.

In general, Ruby programmers tend to favor the the command line for running their code, and they tend to use text editors rather than IDEs.

* Ruby runs on Windows, Linux, and Macs.

Author(s): Dave Thomas, Andy Hunt, Chad Fowler
Edition: 4th
Publisher: Pragmatic Bookshelf
Year: 2013

Language: English
Pages: 888

Cover......Page 1
Table of Contents......Page 5
Foreword to the Third Edition......Page 10
Why Ruby?......Page 11
Resources......Page 12
Acknowledgments......Page 13
Notation Conventions......Page 14
Road Map......Page 15
Part I—Facets of Ruby......Page 17
The Command Prompt......Page 18
Installing Ruby......Page 20
Running Ruby......Page 24
Ruby Documentation: RDoc and ri......Page 26
Ruby Is an Object-Oriented Language......Page 29
Some Basic Ruby......Page 31
Arrays and Hashes......Page 34
Symbols......Page 35
Control Structures......Page 37
Regular Expressions......Page 38
Blocks and Iterators......Page 39
Reading and 'Riting......Page 41
Onward and Upward......Page 42
3. Classes, Objects, and Variables......Page 43
Objects and Attributes......Page 46
Classes Working with Other Classes......Page 51
Access Control......Page 54
Variables......Page 57
Arrays......Page 59
Hashes......Page 61
Blocks and Iterators......Page 66
Containers Everywhere......Page 82
Inheritance and Messages......Page 83
Modules......Page 87
Mixins......Page 89
Composing Modules......Page 91
Inheritance, Mixins, and Design......Page 94
Numbers......Page 96
Strings......Page 99
Ranges......Page 103
What Regular Expressions Let You Do......Page 106
Ruby's Regular Expressions......Page 107
Digging Deeper......Page 109
Advanced Regular Expressions......Page 118
Defining a Method......Page 128
Calling a Method......Page 131
9. Expressions......Page 137
Operator Expressions......Page 138
Miscellaneous Expressions......Page 139
Assignment......Page 140
Conditional Execution......Page 144
case Expressions......Page 148
Loops......Page 150
Variable Scope, Loops, and Blocks......Page 154
The Exception Class......Page 156
Handling Exceptions......Page 157
Raising Exceptions......Page 161
catch and throw......Page 162
Opening and Closing Files......Page 164
Reading and Writing Files......Page 165
Talking to Networks......Page 169
Parsing HTML......Page 170
Fibers......Page 172
Multithreading......Page 174
Mutual Exclusion......Page 178
Running Multiple Processes......Page 181
13. Unit Testing......Page 185
The Testing Framework......Page 187
Structuring Tests......Page 191
Organizing and Running Tests......Page 193
RSpec and Shoulda......Page 196
Test::Unit assertions......Page 203
Ruby Debugger......Page 205
Interactive Ruby......Page 206
Editor Support......Page 207
But It Doesn't Work!......Page 208
But It's Too Slow!......Page 211
Part II—Ruby in Its Setting......Page 216
Command-Line Arguments......Page 217
Environment Variables......Page 222
Where Ruby Finds Its Libraries......Page 224
RubyGems Integration......Page 225
The Rake Build Tool......Page 230
Build Environment......Page 232
Namespaces......Page 233
Organizing Your Source......Page 234
Distributing and Installing Your Code......Page 241
17. Character Encoding......Page 246
Source Files......Page 247
Transcoding......Page 252
Input and Output Encoding......Page 253
Default External Encoding......Page 255
Encoding Compatibility......Page 256
Default Internal Encoding......Page 257
Fun with Unicode......Page 258
Command Line......Page 259
Commands......Page 266
19. Documenting Ruby......Page 269
Adding RDoc to Ruby Code......Page 272
Adding RDoc to C Extensions......Page 275
Running RDoc......Page 277
Ruby source file documented with RDoc......Page 278
C source file documented with RDoc......Page 280
Using cgi.rb......Page 282
Templating Systems......Page 285
Cookies......Page 289
Choice of Web Servers......Page 291
Frameworks......Page 292
Win32API......Page 293
Windows Automation......Page 294
Part III—Ruby Crystallized......Page 299
Source Layout......Page 300
The Basic Types......Page 302
Names......Page 309
Variables and Constants......Page 311
Expressions, Conditionals, and Loops......Page 319
Method Definition......Page 326
Invoking a Method......Page 330
Aliasing......Page 333
Class Definition......Page 334
Module Definitions......Page 336
Blocks, Closures, and Proc Objects......Page 338
Exceptions......Page 342
catch and throw......Page 344
23. Duck Typing......Page 345
Classes Aren't Types......Page 346
Coding like a Duck......Page 350
Standard Protocols and Coercions......Page 351
Walk the Walk, Talk the Talk......Page 357
Objects and Classes......Page 358
Singletons......Page 361
Inheritance and Visibility......Page 366
Modules and Mixins......Page 367
Metaprogramming Class-Level Macros......Page 373
Two Other Forms of Class Definition......Page 378
instance_eval and class_eval......Page 380
Hook Methods......Page 384
One Last Example......Page 389
Top-Level Execution Environment......Page 391
The Turtle Graphics Program......Page 392
Looking at Objects......Page 394
Looking at Classes......Page 395
Calling Methods Dynamically......Page 397
System Hooks......Page 399
Tracing Your Program's Execution......Page 401
Behind the Curtain: The Ruby VM......Page 403
Marshaling and Distributed Ruby......Page 404
Compile Time? Runtime? Anytime!......Page 409
26. Locking Ruby in the Safe......Page 410
Tainted Objects......Page 411
Trusted Objects......Page 412
Definition of the safe levels......Page 413
Part IV—Ruby Library Reference......Page 415
27. Built-in Classes and Modules......Page 416
Array......Page 420
BasicObject......Page 438
Bignum......Page 443
Binding......Page 446
Class......Page 447
Comparable......Page 449
Complex......Page 450
Dir......Page 455
Encoding......Page 461
Enumerable......Page 465
Enumerator......Page 476
Errno......Page 481
Exception......Page 482
FalseClass......Page 485
Fiber......Page 486
File......Page 487
File::Stat......Page 502
FileTest......Page 508
Fixnum......Page 509
Float......Page 512
GC......Page 516
GC::Profiler......Page 518
Hash......Page 520
Integer......Page 531
IO......Page 535
Kernel......Page 555
Marshal......Page 556
MatchData......Page 558
Math......Page 561
Method......Page 564
Module......Page 567
Mutex......Page 587
NilClass......Page 588
Numeric......Page 590
Object......Page 598
ObjectSpace......Page 630
Proc......Page 632
Process......Page 636
Process::GID......Page 642
Process::Status......Page 643
Process::Sys......Page 646
Process::UID......Page 647
Range......Page 649
Random......Page 654
Rational......Page 655
Regexp......Page 658
Signal......Page 663
String......Page 665
Struct......Page 692
Struct::Tms......Page 696
Symbol......Page 697
Thread......Page 701
ThreadGroup......Page 709
Time......Page 711
TracePoint......Page 721
TrueClass......Page 724
UnboundMethod......Page 725
28. Standard Library......Page 727
Abbrev......Page 729
Base64......Page 730
Benchmark......Page 731
BigDecimal......Page 732
CGI......Page 733
CGI::Session......Page 735
Complex......Page 736
Continuation......Page 737
coverage......Page 738
CSV......Page 739
Curses......Page 741
Date/DateTime......Page 742
DBM......Page 743
Delegator......Page 744
Digest......Page 745
dRuby......Page 746
English......Page 747
erb......Page 748
Etc......Page 750
expect......Page 751
Fcntl......Page 752
Fiber......Page 753
Fiddle......Page 754
FileUtils......Page 755
Find......Page 756
Forwardable......Page 757
GDBM......Page 758
GetoptLong......Page 759
IO/console......Page 760
IO/Wait......Page 761
IPAddr......Page 762
irb......Page 763
json......Page 764
Logger......Page 765
mathn......Page 766
MiniTest......Page 767
Monitor......Page 768
Mutex_m......Page 769
Net::FTP......Page 770
Net::HTTP......Page 771
Net::IMAP......Page 773
Net::POP......Page 774
Net::SMTP......Page 775
Net::Telnet......Page 776
NKF......Page 777
Observable......Page 778
open-uri......Page 779
Open3......Page 780
OpenSSL......Page 781
OptionParser......Page 782
OpenStruct......Page 784
Pathname......Page 785
PP......Page 786
PrettyPrint......Page 787
prime......Page 788
Profile......Page 789
Profiler__......Page 790
PStore......Page 791
Rational......Page 792
Resolv......Page 793
REXML......Page 794
Rinda......Page 796
Ripper......Page 797
RSS......Page 799
Scanf......Page 800
SecureRandom......Page 801
Set......Page 802
Shellwords......Page 803
Singleton......Page 804
Socket......Page 805
StringIO......Page 806
StringScanner......Page 807
Syslog......Page 808
Tempfile......Page 809
Test::Unit......Page 810
thread......Page 811
ThreadsWait......Page 812
Time......Page 813
Timeout......Page 814
Tk......Page 815
tmpdir......Page 816
Tracer......Page 817
TSort......Page 818
un......Page 819
URI......Page 820
WeakRef......Page 821
WEBrick......Page 822
WIN32OLE......Page 823
XMLRPC......Page 824
YAML......Page 825
Zlib......Page 826
Web Sites......Page 827
Bug Reporting......Page 828
A2. Bibliography......Page 829
– SYMBOLS –......Page 830
– A –......Page 832
– B –......Page 833
– C –......Page 834
– D –......Page 836
– E –......Page 837
– F –......Page 840
– G –......Page 841
– H –......Page 842
– I –......Page 843
– L –......Page 844
– M –......Page 845
– N –......Page 847
– O –......Page 848
– P –......Page 849
– R –......Page 851
– S –......Page 853
– T –......Page 856
– U –......Page 858
– W –......Page 859
– Z –......Page 860