The Well-Grounded Rubyist
eBook - ePub

The Well-Grounded Rubyist

Joe Leo

Share book
  1. 584 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

The Well-Grounded Rubyist

Joe Leo

Book details
Book preview
Table of contents
Citations

About This Book

Summary The Well-Grounded Rubyist, Third Edition is a beautifully written tutorial that begins with your first Ruby program and takes you all the way to sophisticated topics like reflection, threading, and recursion. Ruby masters David A. Black and Joe Leo distill their years of knowledge for you, concentrating on the language and its uses so you can use Ruby in any way you choose. Updated for Ruby 2.5.Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology Designed for developer productivity, Ruby is an easy-to-learn dynamic language perfect for creating virtually any kind of software. Its famously friendly development community, countless libraries, and amazing tools, like the Rails framework, have established it as the language of choice for high-profile companies, including GitHub, SlideShare, and Shopify. The future is bright for the well-grounded Rubyist! About the Book In The Well-Grounded Rubyist, Third Edition, expert authors David A. Black and Joseph Leo deliver Ruby mastery in an easy-to-read, casual style. You'll lock in core principles as you write your first Ruby programs. Then, you'll progressively build up to topics like reflection, threading, and recursion, cementing your knowledge with high-value exercises to practice your skills along the way. What's Inside

  • Basic Ruby syntax
  • Running Ruby extensions
  • FP concepts like currying, side-effect-free code, and recursion
  • Ruby 2.5 updates

About the Reader For readers with beginner-level programming skills. About the Authors David A. Black is an internationally known Ruby developer and author, and a cofounder of Ruby Central. Ruby teacher and advocate Joseph Leo III is the founder of Def Method and lead organizer of the Gotham Ruby Conference. Table of Contents

PART 1 RUBY FOUNDATIONS

  • 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

PART 2 BUILT-IN CLASSES AND MODULES

  • 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

PART 3 RUBY DYNAMICS

  • Object individuation
  • Callable and runnable objects
  • Callbacks, hooks, and runtime introspection
  • Ruby and functional programming

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is The Well-Grounded Rubyist an online PDF/ePUB?
Yes, you can access The Well-Grounded Rubyist by Joe Leo in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Ruby. We have over one million books available in our catalogue for you to explore.

Information

Publisher
Manning
Year
2019
ISBN
9781638356233

Part 1. Ruby foundations

The goal of this part of the book is to give you a broad but practical foundation layer on which to build, and to which to anchor, the further explorations of Ruby that follow in parts 2 and 3. We’ll start with a chapter on bootstrapping your Ruby literacy; after working through that first chapter, you’ll be able to run Ruby programs comfortably and have a good sense of the layout of a typical Ruby installation. Starting with chapter 2, we’ll get into the details of the Ruby language. Ruby is an object-oriented language, and the sooner you dive into how Ruby handles objects, the better. Accordingly, objects will serve both as a way to bootstrap the discussion of the language (and your knowledge of it) and as a golden thread leading us to further topics and techniques.
Objects are created by classes, and in chapter 3 you’ll learn how classes work. The discussion of classes is followed by a look at modules in chapter 4. Modules allow you to fine-tune classes and objects by splitting out some of the object design into separate, reusable units of code. To understand Ruby programs—both your own and others’—you need to know about Ruby’s notion of a current default object, known by the keyword self. Chapter 5 will take you deep into the concept of self, along with a treatment of Ruby’s handling of variable visibility and scope.
In chapter 6, the last in this part of the book, you’ll learn about control flow in Rubyprograms—that is, how to steer the Ruby interpreter through conditional (if) logic, how to loop repeatedly through code, and even how to break away from normal program execution when an error occurs. By the end of chapter 6, you’ll be thinking along with Ruby as you write and develop your code.
The title of this part is “Ruby foundations,” which obviously suggests that what’s here is to be built on later. And that’s true. But it doesn’t mean that the material in part 1 isn’t important in itself. As you’ll see once you read them, these six chapters present you with real Ruby techniques, real code, and information you’ll use every time you write or execute a Ruby program. It’s not the “foundations” because you’ll learn it once and then ignore it, but because there’s so much more about Ruby yet to follow!

Chapter 1. Bootstrapping your Ruby literacy

This chapter covers
  • A Ruby syntax survival kit
  • Writing, saving, running, and error-checking programs
  • A tour of the Ruby installation
  • The mechanics of Ruby extensions
  • Ruby’s command-line tools such as irb and rake
This book will give you a foundation in Ruby, and this chapter will give your foundation a foundation. The goal of the chapter is to bootstrap you into the study of Ruby with enough knowledge and skill to proceed comfortably into what lies beyond.
We’ll look at basic Ruby syntax and techniques and at how Ruby works: what you do when you write a program, how you get Ruby to run your program, and how you split a program into more than one file. You’ll learn several of the switches that alter how the Ruby interpreter (the program with the name ruby, to which you feed your program files for execution) acts, as well as how to use some important auxiliary tools designed to make your life as a Rubyist easier and more productive.
The chapter is based on a view of the whole Ruby landscape as being divided into three fundamental levels:
  • Core language: design principles, syntax, and semantics
  • Extensions and libraries that ship with Ruby, and the facilities for adding extensions of your own
  • Command-line tools that come with Ruby, with which you run the interpreter and some other important utilities
It’s not always possible to talk about these three levels in isolation—after all, they’re interlocking parts of a single system—but we’ll discuss them separately as much as possible in this chapter. You can, in any case, use the three level descriptions as pegs to hang subtopics on, wherever they’re introduced.
Ruby, ruby, and ... RUBY?!
Ruby is a programming language. We talk about things like “learning Ruby,” and we ask questions like, “Do you know Ruby?”
The lowercase version, ruby, is a computer program. Specifically, it’s the Ruby interpreter, the program that reads your programs and runs them. You’ll see this name used in sentences like “I ran ruby on my file, but nothing happened,” or “What’s the full path to your ruby executable?”
Finally, there’s RUBY—or, more precisely, there isn’t. Ruby isn’t an acronym, and it’s never correct to spell it in all capital letters. People do this, as they do (also incorrectly) 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.

1.1. Basic Ruby language literacy

The goal of this section is to get you going with Ruby. It takes a breadth-first approach: we’ll walk through the whole cycle of learning some syntax, writing some code, and running some programs.

1.1.1. Installing Ruby and using a text editor

Though you’re free to install and compile Ruby from source from www.ruby-lang.org, it’s far more common for Rubyists using macOS or Linux to install versions of Ruby using a version manager. The most popular version managers are RVM (https://rvm.io), rbenv (https://github.com/rbenv/rbenv), and chruby (https://github.com/postmodern/chruby). Windows users are encouraged to use the RubyInstaller (https://rubyinstaller.org/). All version managers are free and all provide a safe and easy way to download and run Ruby. This book references Ruby version 2.5.1.
You’ll also need a text editor (any editor you like, as long as it’s a plain-text editor and not a word processor) and a directory (a.k.a. a folder) in which to store your Ruby program files. You might name that directory rubycode or rubysamples—any name is fine. Keep it separate from other work areas so that you can keep track of your practice program files.
The interactive Ruby console program (irb), your new best friend
The irb utility ships with Ruby and is the most widely used Ruby command-line tool other than the interpreter itself. After starting irb, you type Ruby code into it, and it executes the code and prints out the resulting value.
Type irb at the command line and enter sample code as you encounter it in the text. For example:
>> 100 + 32 => 132
Having an open irb session means you can test Ruby snippets at any time and in any quantity. Most Ruby developers find irb indispensable, and you’ll see a few examples of its use as we proceed through this chapter.
To exit from irb normally, you can type exit. On many systems, Ctrl-D works too.
The irb examples you’ll see in this book use a command-line option that makes irb output easier to read:
irb --simple-prompt
If you want to see the effect of the --simple-prompt option, try starting irb with and without it. As you’ll see, the simple prompt keeps your screen a lot clearer. The default (nonsimple) prompt displays more information, such as a line-number count for your interactive session; but for the examples we’ll look at, the simple prompt is sufficient.
Because irb is one of the command-line tools that ship with Ruby, it’s not discussed in detail until section 1.4.2. Feel free to jump to that section and have a look; i...

Table of contents