The Well-Grounded Rubyist
David A. Black
Format: PDF / Kindle (mobi) / ePub
The Well-Grounded Rubyist, Second Edition addresses both newcomers to Ruby as well as Ruby programmers who want to deepen their understanding of the language. This beautifully written and totally revised second edition includes coverage of features that are new in Ruby 2.1, as well as expanded and updated coverage of aspects of the language that have changed.
Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.
About the Technology
This is a good time for Ruby! It's powerful like Java or C++, and has dynamic features that let your code react gracefully to changes at runtime. And it's elegant, so creating applications, development tools, and administrative scripts is easier and more straightforward. With the long-awaited Ruby 2, an active development community, and countless libraries and productivity tools, Ruby has come into its own.
About the Book
The Well-Grounded Rubyist, Second Edition is a beautifully written tutorial that begins with your first Ruby program and goes on to explore sophisticated topics like callable objects, reflection, and threading. The book concentrates on the language, preparing you to use Ruby in any way you choose. This second edition includes coverage of new Ruby features such as keyword arguments, lazy enumerators, and Module#prepend, along with updated information on new and changed core classes and methods.
- Clear explanations of Ruby concepts
- Numerous simple examples
- Updated for Ruby 2.1
- Prepares you to use Ruby anywhere for any purpose
About the Author
David A. Black is an internationally known Ruby developer, author, trainer, speaker, event organizer, and founder of Ruby Central, as well as a Lead Consultant at Cyrus Innovation.
Table of Contents
- Bootstrapping your Ruby literacy
- Objects, methods, and local variables
- Organizing objects with classes
- Modules and program organization
- The default object (self), scope, and visibility
- Control-flow techniques
- Built-in essentials
- Strings, symbols, and other scalar objects
- Collection and container objects
- Collections central: Enumerable and Enumerator
- Regular expressions and regexp-based string operations
- File and I/O operations
- Object individuation
- Callable and runnable objects
- Callbacks, hooks, and runtime introspection
PART 1 RUBY FOUNDATIONS
PART 2 BUILT-IN CLASSES AND MODULES
PART 3 RUBY DYNAMICS
automating the creation of temporary files tk.rb—A programming interface to the Tk graphics library Licensed to sam kaplan
Listing 4.3 starts with the innocent-looking (but powerful) instantiation B of a new Stack object, which we’ve assigned to the variable s. That Stack object is born with the knowledge of what to do when we ask it to perform stack-related actions, thanks to the fact that its class mixed in the Stacklike module. The rest of the code involves asking it to jump through some stacklike hoops: adding items (strings) to itself C and popping the last one off itself D. Along the way, we ask the object to
cornerstones of Ruby programming—the backbone, in some respects—is the default object or current object, accessible to you in your program through the keyword self. At every point when your program is running, there is one and only one self. Being self has certain privileges, as you’ll see. In this section, we’ll look at how Ruby determines which object is self at a given point and what privileges are granted to the object that is self. Licensed to sam kaplan
privilege of being the owner of instance variables. 5.1.4 Resolving instance variables through self A simple rule governs instance variables and their resolution: every instance variable you’ll ever see in a Ruby program belongs to whatever object is the current object (self) at that point in the program. Here’s a classic case where this knowledge comes in handy. See if you can figure out what this code will print, before you run it: class C def show_var @v = "I am an instance variable
it’s never correct to spell it in all capital letters. People do this, as they do (also wrongly) with Perl, perhaps because they’re used to seeing language names like BASIC and COBOL. Ruby isn’t such a language. It’s Ruby for the language, ruby for the interpreter. Nor does this first chapter exist solely in the service of later chapters. It has content in its own right: you’ll learn real Ruby techniques and important points about the design of the language. The goal is to bootstrap or