You're wondering what this Ruby language is all about, and if it's right for you. Let us ask you this: Do you like being productive? Do you feel like all those extra compilers and libraries and class files and keystrokes in your other language bring you closer to a finished product, admiring coworkers, and happy customers? Would you like a language that takes care of the details for you? If you sometimes wish you could stop maintaining boilerplate code and get to work on your problem, then Ruby is for you. Ruby lets you get more done with less code.
You’ve been calling methods and creating objects like a pro. But the only methods you could call, and the only kinds of objects you could create, were the ones that Ruby defined for you.
Now, it’s your turn. You’re going to learn to create your own methods. You’ll also create your own classes—templates for new objects. You’ll decide what objects based on your class will be like. You’ll use instance variables to define what those objects know, and instance methods to define what they do. And most importantly, you’ll discover how defining your own classes can make your code easier to read and maintain.
Your new classes representing the different types of vehicles and animals are awesome, it’s true. But you’re having to copy instance methods from class to class. And the copies are starting to fall out of sync—some are fine, while others have bugs. Weren’t classes supposed to make code easier to maintain?
In this chapter, we’ll learn how to use inheritance to let your classes share methods. Fewer copies means fewer maintenance headaches!
Every instance you create starts out as a clean slate. If you call certain instance methods before adding data, an error will be raised that will bring your whole program to a screeching halt. In this chapter, we’re going to show you a couple of ways to create objects that are safe to use right away. We’ll start with the
initialize method, which lets you pass in a bunch of arguments to set up an object’s data at the time you create it. Then we’ll show you how to write class methods, which you can use to create and set up an object even more easily.
A whole lot of programming deals with lists of things. Lists of addresses. Lists of phone numbers. Lists of products. Matz, the creator of Ruby, knew this. So he worked really hard to make sure that working with lists in Ruby is really easy. First, he ensured that arrays, which keep track of lists in Ruby, have lots of powerful methods to do almost anything you might need with a list. Second, he realized that writing code to loop over a list to do something with each item, although tedious, is something developers were doing a lot. So he added blocks to the language, and removed the need for all that looping code. What is a block, exactly? Read on to find out...
Up until now, you've just seen methods handing data off to a block, and expecting the block to handle everything. But a block can also return data to the method. This feature lets the method get directions from the block, allowing it to do more of the work.
In this chapter, we’ll show you some methods that will let you take a big, complicated collection, and use block return values to cut it down to size.
You’ve already seen how to create a collection of objects using an array. You’ve seen how to process each item in an array, and how to find items you want. In both cases, you start at the beginning of the array, and look through Every. Single. Object. You’ve also seen methods that take big collections of parameters. You’ve seen the problems this causes: method calls require a big, confusing collection of arguments that you have to remember the exact order for.
What if there were a kind of collection where all the data had labels on it? You could quickly find the elements you needed! In this chapter, we’ll learn about Ruby hashes, which do just that.
Ever sent an email to the wrong contact? You probably had a hard time sorting out the confusion that ensued. Well, Ruby objects are just like those contacts in your address book, and calling methods on them is like sending messages to them. If your address book gets mixed up, it’s possible to send messages to the wrong object. This chapter will help you recognize the signs that this is happening, and help you get your programs running smoothly again.
You can only inherit methods from one class. But what if you need to share several sets of behavior across several classes? Like methods for starting a battery charge cycle and reporting its charge level—you might need those methods on phones, power drills, and electric cars. Are you going to create a single superclass for all of those? (It won’t end well if you try.) Or methods for starting and stopping a motor. Sure, the drill and the car might need those, but the phone won’t!
In this chapter, we’ll learn about modules and mixins, a powerful way to group methods together and then share them only with particular classes that need them.
You’ve seen that mixins can be useful. But you haven’t seen their full power yet. The Ruby core library includes two mixins that will blow your mind.
Comparable, is used for comparing objects. You’ve used operators like
== on numbers and strings, but
Comparable will let you use them on your classes.
The second mixin,
Enumerable, is used for working with collections. Remember those super-useful
map methods that you used on arrays before? Those came from
Enumerable. But that’s a tiny fraction of what
Enumerable can do. And again, you can mix it into your classes. Read on to see how!
There’s an old saying: “Give someone a fish, and you feed them for a day. Teach them how to fish, and you feed them for a lifetime.” We’ve been giving you fish so far. We’ve shown you how to use a few of Ruby’s classes and modules. But there are dozens more, some of them applicable to your problems, that we don’t have room to cover. So it’s time to teach you how to fish. There’s excellent documentation freely available on all of Ruby’s classes, modules, and methods. You just have to know where to find it, and how to interpret it. That’s what this chapter will show you.
In the real world, the unexpected happens. Someone could delete the file your program is trying to load, or the server your program is trying to contact could go down. Your code could check for these exceptional situations, but those checks would be mixed in with the code that handles normal operation. (And that would be a big, unreadable mess.)
This chapter will teach you all about Ruby’s exception handling, which lets you write code to handle the unexpected, and keep it separate from your regular code.
Are you sure your software is working right now? Really sure? Before you sent that new version to your users, you presumably tried out the new features to ensure they all worked. But did you try the old features to ensure you didn’t break any of them? All the old features? If that question makes you uneasy, your program needs automated testing. Automated tests ensure your program’s components work correctly, even after you change your code.
Unit tests are the most common, most important type of automated test. And Ruby includes
MiniTest, a library devoted to unit testing. This chapter will teach you everything you need to know about it!
This is the 21st century. Users want web apps. Ruby’s got you covered there, too! Libraries are available to help you host your own web applications and make them accessible from any web browser. So we’re going to spend these final two chapters of the book showing you how to build a full web app.
To get started, you’re going to need
Sinatra, a third-party library for writing web applications. But don’t worry, we’ll show you how to use the RubyGems tool (included with Ruby) to download and install libraries automatically! Then we’ll show you just enough HTML to create your own web pages. And of course, we’ll show you how to serve those pages to a browser!
We’ve covered a lot of ground, and you’re almost finished with this book. We’ll miss you, but before we let you go, we wouldn’t feel right about sending you out into the world without a little more preparation. We can’t possibly fit everything you’ll need to know about Ruby into these few pages... But we’ve kept all the best bits for this Top Ten appendix.