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 1.9 is the latest version of Ruby. It has many compelling features, including multinationalization support, new block scoping, and better performance. At the same time, the current Ruby 1.8 is still being maintained and is widely used. We're therefore releasing both the Ruby 1.9 version of the PickAxe (this book) alongside the 1.8 version of the PickAxe. Readers can choose the book that matches their environment. 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, making Ruby a favorite tool of intelligent, forward-thinking programmers and the basis for the Rails web framework. 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). 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, multinationalization, and the new block declaration syntax, among other exciting new features. Foreword......Page 16 Preface......Page 17 Road Map......Page 22 Part I---Facets of Ruby......Page 24 The Command Prompt......Page 25 Installing Ruby......Page 27 Running Ruby......Page 30 Ruby Documentation: RDoc and ri......Page 32 Ruby Is an Object-Oriented Language......Page 35 Some Basic Ruby......Page 37 Arrays and Hashes......Page 40 Symbols......Page 42 Control Structures......Page 43 Regular Expressions......Page 45 Blocks and Iterators......Page 46 Reading and 'Riting......Page 48 Onward and Upward......Page 49 Classes, Objects, and Variables......Page 50 Objects and Attributes......Page 53 Classes Working with Other Classes......Page 58 Access Control......Page 61 Variables......Page 64 Containers, Blocks, and Iterators......Page 67 Blocks and Iterators......Page 74 Containers Everywhere......Page 90 Sharing Functionality: Inheritance, Modules, and Mixins......Page 91 Inheritance and Messages......Page 97 Modules......Page 96 Mixins......Page 98 Iterators and the Enumerable Module......Page 100 Composing Modules......Page 101 Inheritance, Mixins, and Design......Page 104 Numbers......Page 106 Strings......Page 109 Ranges......Page 114 What Regular Expressions Let You Do......Page 117 Ruby's Regular Expressions......Page 118 Digging Deeper......Page 120 Pattern-Based Substitution......Page 128 Advanced Regular Expressions......Page 130 Defining a Method......Page 137 Calling a Method......Page 140 Expressions......Page 146 Operator Expressions......Page 147 Miscellaneous Expressions......Page 149 Assignment......Page 150 Conditional Execution......Page 153 Case Expressions......Page 158 Loops......Page 160 Variable Scope, Loops, and Blocks......Page 165 The Exception Class......Page 167 Handling Exceptions......Page 168 Raising Exceptions......Page 172 Catch and Throw......Page 174 What Is an IO Object?......Page 176 Opening and Closing Files......Page 177 Reading and Writing Files......Page 178 Talking to Networks......Page 181 Fibers......Page 184 Multithreading......Page 186 Controlling the Thread Scheduler......Page 190 Mutual Exclusion......Page 191 Running Multiple Processes......Page 194 Unit Testing......Page 198 The Testing Framework......Page 200 Structuring Tests......Page 204 Organizing and Running Tests......Page 206 RSpec and Shoulda......Page 209 Ruby Debugger......Page 220 Interactive Ruby......Page 221 Editor Support......Page 222 But It Doesn't Work!......Page 224 But It's Too Slow!......Page 227 Part II---Ruby in Its Setting......Page 232 Command-Line Arguments......Page 233 Program Termination......Page 236 Environment Variables......Page 237 Where Ruby Finds Its Libraries......Page 238 RubyGems Integration......Page 239 The Rake Build Tool......Page 245 Build Environment......Page 248 Namespaces......Page 249 Organizing Your Source......Page 251 Distributing and Installing Your Code......Page 258 Character Encoding......Page 264 Encodings......Page 265 Source Files......Page 266 Transcoding......Page 270 Input and Output Encoding......Page 272 Default External Encoding......Page 274 Encoding Compatibility......Page 275 Default Internal Encoding......Page 276 Fun with Unicode......Page 277 Command Line......Page 278 Configuration......Page 282 Commands......Page 286 Restrictions......Page 288 Documenting Ruby......Page 290 Adding RDoc to Ruby Code......Page 293 Adding RDoc to C Extensions......Page 298 Running RDoc......Page 299 Writing CGI Scripts......Page 304 Cookies......Page 313 Choice of Web Servers......Page 314 Frameworks......Page 315 Ruby and Microsoft Windows......Page 316 Windows Automation......Page 318 Win32API......Page 317 Part III---Ruby Crystallized......Page 324 Source Layout......Page 325 The Basic Types......Page 327 Names......Page 334 Variables and Constants......Page 336 Expressions......Page 344 Method Definition......Page 351 Invoking a Method......Page 355 Class Definition......Page 358 Module Definitions......Page 360 Access Control......Page 362 Blocks, Closures, and Proc Objects......Page 363 Exceptions......Page 367 Catch and Throw......Page 369 Duck Typing......Page 370 Classes Aren't Types......Page 371 Coding like a Duck......Page 375 Standard Protocols and Coercions......Page 376 Walk the Walk, Talk the Talk......Page 383 Objects and Classes......Page 384 Singletons......Page 387 Inheritance and Visibility......Page 393 Modules and Mixins......Page 394 Metaprogramming Class-Level Macros......Page 397 Two Other Forms of Class Definition......Page 402 instance_eval and class_eval......Page 406 Hook Methods......Page 410 One Last Example......Page 415 Top-Level Execution Environment......Page 417 The Turtle Graphics Program......Page 418 Looking at Objects......Page 420 Looking at Classes......Page 422 Calling Methods Dynamically......Page 423 System Hooks......Page 426 Tracing Your Program's Execution......Page 427 Behind the Curtain: The Ruby VM......Page 430 Marshaling and Distributed Ruby......Page 431 Compile Time? Runtime? Anytime!......Page 435 Locking Ruby in the Safe......Page 436 Safe Levels......Page 437 Trusted Objects......Page 438 Part IV---Ruby Library Reference......Page 441 Built-in Classes and Modules......Page 442 Alphabetical Listing......Page 443 Array......Page 447 BasicObject......Page 463 Bignum......Page 466 Binding......Page 469 Class......Page 470 Comparable......Page 472 Complex......Page 473 Dir......Page 478 Encoding......Page 483 Enumerable......Page 487 Enumerator......Page 496 Errno......Page 500 Exception......Page 501 FalseClass......Page 504 Fiber......Page 505 File......Page 506 File::Stat......Page 518 FileTest......Page 524 Fixnum......Page 525 Float......Page 528 GC......Page 532 Hash......Page 533 Integer......Page 543 IO......Page 546 Kernel......Page 564 Marshal......Page 583 MatchData......Page 585 Math......Page 588 Method......Page 591 Module......Page 594 Mutex......Page 612 NilClass......Page 613 Numeric......Page 615 Object......Page 622 ObjectSpace......Page 635 Proc......Page 637 Process......Page 641 Process::GID......Page 648 Process::Status......Page 650 Process::Sys......Page 653 Process::UID......Page 655 Range......Page 656 Rational......Page 660 Regexp......Page 663 Signal......Page 668 String......Page 670 Struct......Page 696 Struct::Tms......Page 700 Symbol......Page 701 Thread......Page 705 ThreadGroup......Page 712 Time......Page 714 TrueClass......Page 723 UnboundMethod......Page 724 Standard Library......Page 726 Library Changes in Ruby 1.9......Page 727 Abbrev......Page 729 Base64......Page 730 Benchmark......Page 731 BigDecimal......Page 732 CGI......Page 733 CGI::Session......Page 735 CMath......Page 736 Complex......Page 737 Continuation......Page 738 CSV......Page 739 Curses......Page 741 Date/DateTime......Page 742 DBM......Page 743 Delegator......Page 744 Digest......Page 745 DL......Page 746 dRuby......Page 747 English......Page 748 erb......Page 749 Etc......Page 751 expect......Page 752 Fcntl......Page 753 Fiber......Page 754 FileUtils......Page 755 Find......Page 756 Forwardable......Page 757 GDBM......Page 758 GetoptLong......Page 759 GServer......Page 760 Iconv......Page 761 IO/Wait......Page 762 IPAddr......Page 763 irb......Page 764 json......Page 765 Logger......Page 766 mathn......Page 767 Matrix......Page 769 MiniTest......Page 770 Monitor......Page 771 Mutex_m......Page 772 Net::FTP......Page 773 Net::HTTP......Page 774 Net::IMAP......Page 776 Net::POP......Page 777 Net::SMTP......Page 778 Net::Telnet......Page 779 NKF......Page 780 Observable......Page 781 open-uri......Page 782 Open3......Page 783 OpenSSL......Page 784 OptionParser......Page 785 OpenStruct......Page 787 Pathname......Page 788 PP......Page 789 PrettyPrint......Page 790 prime......Page 791 Profile......Page 792 Profiler__......Page 793 PStore......Page 794 PTY......Page 795 Rational......Page 796 Readline......Page 797 Resolv......Page 798 REXML......Page 799 Rinda......Page 801 Ripper......Page 802 RSS......Page 804 Scanf......Page 805 SDBM......Page 806 SecureRandom......Page 807 Set......Page 808 Shellwords......Page 809 Singleton......Page 810 Socket......Page 811 StringIO......Page 812 StringScanner......Page 813 Syslog......Page 814 Tempfile......Page 815 Test::Unit......Page 816 thread......Page 817 ThreadsWait......Page 818 Time......Page 819 Timeout......Page 820 Tk......Page 821 tmpdir......Page 822 Tracer......Page 823 TSort......Page 824 un......Page 825 URI......Page 826 WeakRef......Page 827 WEBrick......Page 828 WIN32OLE......Page 829 XMLRPC......Page 830 YAML......Page 831 Zlib......Page 832 Your First Extension......Page 833 Ruby Objects in C......Page 836 The Threading Model......Page 841 The Jukebox Extension......Page 845 Memory Allocation......Page 852 Ruby Type System......Page 853 Creating an Extension......Page 855 Embedding a Ruby Interpreter......Page 860 Bridging Ruby to Other Environments......Page 864 Ruby C Language API......Page 865 mkmf......Page 874 Part V---Appendixes......Page 877 Socket Library......Page 878 BasicSocket......Page 879 Socket......Page 881 IPSocket......Page 885 TCPSocket......Page 886 SOCKSSocket......Page 887 TCPServer......Page 888 UDPSocket......Page 889 UNIXSocket......Page 891 UNIXServer......Page 892 Websites......Page 893 Mailing Lists......Page 894 Bug Reporting......Page 895 Bibliography......Page 896 Symbols......Page 897 A......Page 899 B......Page 901 C......Page 902 D......Page 905 E......Page 907 F......Page 909 G......Page 911 H......Page 912 I......Page 913 J......Page 914 L......Page 915 M......Page 916 N......Page 919 O......Page 920 P......Page 921 R......Page 923 S......Page 926 T......Page 930 U......Page 932 W......Page 933 Z......Page 934 The Pragmatic Bookshelf Foreword 16 Preface 17 Road Map 22 Part I---Facets of Ruby 24 Getting Started 25 The Command Prompt 25 Installing Ruby 27 Running Ruby 30 Ruby Documentation: RDoc and ri 32 Ruby.new 35 Ruby Is an Object-Oriented Language 35 Some Basic Ruby 37 Arrays and Hashes 40 Symbols 42 Control Structures 43 Regular Expressions 45 Blocks and Iterators 46 Reading and 'Riting 48 Command-Line Arguments 49 Onward and Upward 49 Classes, Objects, and Variables 50 Objects and Attributes 53 Classes Working with Other Classes 58 Access Control 61 Variables 64 Containers, Blocks, and Iterators 67 Blocks and Iterators 74 Containers Everywhere 90 Sharing Functionality: Inheritance, Modules, and Mixins 91 Inheritance and Messages 97 Modules 96 Mixins 98 Iterators and the Enumerable Module 100 Composing Modules 101 Inheritance, Mixins, and Design 104 Standard Types 106 Numbers 106 Strings 109 Ranges 114 Regular Expressions 117 What Regular Expressions Let You Do 117 Ruby's Regular Expressions 118 Digging Deeper 120 Pattern-Based Substitution 128 Advanced Regular Expressions 130 More About Methods 137 Defining a Method 137 Calling a Method 140 Expressions 146 Operator Expressions 147 Miscellaneous Expressions 149 Assignment 150 Conditional Execution 153 Case Expressions 158 Loops 160 Variable Scope, Loops, and Blocks 165 Exceptions, Catch, and Throw 167 The Exception Class 167 Handling Exceptions 168 Raising Exceptions 172 Catch and Throw 174 Basic Input and Output 176 What Is an IO Object? 176 Opening and Closing Files 177 Reading and Writing Files 178 Talking to Networks 181 Fibers, Threads, and Processes 184 Fibers 184 Multithreading 186 Controlling the Thread Scheduler 190 Mutual Exclusion 191 Running Multiple Processes 194 Unit Testing 198 The Testing Framework 200 Structuring Tests 204 Organizing and Running Tests 206 RSpec and Shoulda 209 When Trouble Strikes 220 Ruby Debugger 220 Interactive Ruby 221 Editor Support 222 But It Doesn't Work! 224 But It's Too Slow! 227 Part II---Ruby in Its Setting 232 Ruby and Its World 233 Command-Line Arguments 233 Program Termination 236 Environment Variables 237 Where Ruby Finds Its Libraries 238 RubyGems Integration 239 The Rake Build Tool 245 Build Environment 248 Namespaces, Source Files, and Distribution 249 Namespaces 249 Organizing Your Source 251 Distributing and Installing Your Code 258 Character Encoding 264 Encodings 265 Source Files 266 Transcoding 270 Input and Output Encoding 272 Default External Encoding 274 Encoding Compatibility 275 Default Internal Encoding 276 Fun with Unicode 277 Interactive Ruby Shell 278 Command Line 278 Configuration 282 Commands 286 Restrictions 288 Documenting Ruby 290 Adding RDoc to Ruby Code 293 Adding RDoc to C Extensions 298 Running RDoc 299 Ruby and the Web 304 Writing CGI Scripts 304 Cookies 313 Choice of Web Servers 314 Frameworks 315 Ruby and Microsoft Windows 316 Getting Ruby for Windows 318 Running Ruby Under Windows 317 Win32API 317 Windows Automation 318 Part III---Ruby Crystallized 324 The Ruby Language 325 Source File Encoding 325 Source Layout 325 The Basic Types 327 Names 334 Variables and Constants 336 Expressions 344 Method Definition 351 Invoking a Method 355 Aliasing 358 Class Definition 358 Module Definitions 360 Access Control 362 Blocks, Closures, and Proc Objects 363 Exceptions 367 Catch and Throw 369 Duck Typing 370 Classes Aren't Types 371 Coding like a Duck 375 Standard Protocols and Coercions 376 Walk the Walk, Talk the Talk 383 Metaprogramming 384 Objects and Classes 384 Singletons 387 Inheritance and Visibility 393 Modules and Mixins 394 Metaprogramming Class-Level Macros 397 Two Other Forms of Class Definition 402 instance_eval and class_eval 406 Hook Methods 410 One Last Example 415 Top-Level Execution Environment 417 The Turtle Graphics Program 418 Reflection, ObjectSpace, and Distributed Ruby 420 Looking at Objects 420 Looking at Classes 422 Calling Methods Dynamically 423 System Hooks 426 Tracing Your Program's Execution 427 Behind the Curtain: The Ruby VM 430 Marshaling and Distributed Ruby 431 Compile Time? Runtime? Anytime! 435 Locking Ruby in the Safe 436 Safe Levels 437 Tainted Objects 438 Trusted Objects 438 Part IV---Ruby Library Reference 441 Built-in Classes and Modules 442 Alphabetical Listing 443 Array 447 BasicObject 463 Bignum 466 Binding 469 Class 470 Comparable 472 Complex 473 Dir 478 Encoding 483 Enumerable 487 Enumerator 496 Errno 500 Exception 501 FalseClass 504 Fiber 505 File 506 File::Stat 518 FileTest 524 Fixnum 525 Float 528 GC 532 Hash 533 Integer 543 IO 546 Kernel 564 Marshal 583 MatchData 585 Math 588 Method 591 Module 594 Mutex 612 NilClass 613 Numeric 615 Object 622 ObjectSpace 635 Proc 637 Process 641 Process::GID 648 Process::Status 650 Process::Sys 653 Process::UID 655 Range 656 Rational 660 Regexp 663 Signal 668 String 670 Struct 696 Struct::Tms 700 Symbol 701 Thread 705 ThreadGroup 712 Time 714 TrueClass 723 UnboundMethod 724 Standard Library 726 Library Changes in Ruby 1.9 727 Abbrev 729 Base64 730 Benchmark 731 BigDecimal 732 CGI 733 CGI::Session 735 CMath 736 Complex 737 Continuation 738 CSV 739 Curses 741 Date/DateTime 742 DBM 743 Delegator 744 Digest 745 DL 746 dRuby 747 English 748 erb 749 Etc 751 expect 752 Fcntl 753 Fiber 754 FileUtils 755 Find 756 Forwardable 757 GDBM 758 GetoptLong 759 GServer 760 Iconv 761 IO/Wait 762 IPAddr 763 irb 764 json 765 Logger 766 mathn 767 Matrix 769 MiniTest 770 Monitor 771 Mutex_m 772 Net::FTP 773 Net::HTTP 774 Net::IMAP 776 Net::POP 777 Net::SMTP 778 Net::Telnet 779 NKF 780 Observable 781 open-uri 782 Open3 783 OpenSSL 784 OptionParser 785 OpenStruct 787 Pathname 788 PP 789 PrettyPrint 790 prime 791 Profile 792 Profiler__ 793 PStore 794 PTY 795 Rational 796 Readline 797 Resolv 798 REXML 799 Rinda 801 Ripper 802 RSS 804 Scanf 805 SDBM 806 SecureRandom 807 Set 808 Shellwords 809 Singleton 810 Socket 811 StringIO 812 StringScanner 813 Syslog 814 Tempfile 815 Test::Unit 816 thread 817 ThreadsWait 818 Time 819 Timeout 820 Tk 821 tmpdir 822 Tracer 823 TSort 824 un 825 URI 826 WeakRef 827 WEBrick 828 WIN32OLE 829 XMLRPC 830 YAML 831 Zlib 832 Extending Ruby 833 Your First Extension 833 Ruby Objects in C 836 The Threading Model 841 The Jukebox Extension 845 Memory Allocation 852 Ruby Type System 853 Creating an Extension 855 Embedding a Ruby Interpreter 860 Bridging Ruby to Other Environments 864 Ruby C Language API 865 MKMF Reference 874 mkmf 874 Part V---Appendixes 877 Socket Library 878 BasicSocket 879 Socket 881 IPSocket 885 TCPSocket 886 SOCKSSocket 887 TCPServer 888 UDPSocket 889 UNIXSocket 891 UNIXServer 892 Support 893 Websites 893 Usenet Newsgroup 894 Mailing Lists 894 Bug Reporting 895 Bibliography 896 Index 897 Symbols 897 A 899 B 901 C 902 D 905 E 907 F 909 G 911 H 912 I 913 J 914 K 915 L 915 M 916 N 919 O 920 P 921 Q 923 R 923 S 926 T 930 U 932 V 933 W 933 X 934 Y 934 Z 934 ISBN-13:,9781934356081 I've been using Rails for a few years, but I never dug deeply into Ruby. This book is one of the best technical books I have read, it keeps you engaged, which is very touch to do with such a dry subject matter. I read cover to cover (except the reference in the back) and I feel very confident in my Ruby skills. I learned something new in every chapter, and have a more grounded understanding of Ruby. I have already used this book as a reference, and I expect to do the same for quite a while.If you are using Ruby in any way, this book should be in your library. A tutorial and reference to the object-oriented programming language for beginning to experienced programmers, updated for version 1.9, describes the language's structure, syntax, and operation, and explains how to build applications.