Monday, January 11, 2010

Chapter 2. A Quick Tour of Ruby


A Quick Tour of Ruby > Ruby Is Object-Oriented




Chapter 2. A Quick Tour of Ruby


Without going into all the details, this chapter introduces you to the fundamentals of Ruby: classes and modules, including the Object class and the Kernel module, reserved words (keywords), comments, variables, methods, and so forth. Most topics will be dealt with elsewhere in the book in more detail. Some topics merit entire chapters, others only sections (found in Chapter 10). I'll always tell you where else to look for more information on a topic. This book's most detailed discussions on methods and blocks are found in this chapter.


2.1. Ruby Is Object-Oriented


Matz, the creator of Ruby, had wanted to create his own programming language since he was in high school. He wanted to create a scripting language, but he also wanted it to be object-oriented.


Ruby goes beyond mere scripting, though its programs may look like shell scripts. It is not just a procedural language, but it can be used like one.


Ruby has classes. Classes hold data—in the form of variables and constants—and methods, which are compact collections of code that help you perform operations on data. Classes can inherit information from each other, but only one at a time. This allows you to reuse code—which means you'll spend less time fixing or debugging code—and intermix the code through inheritance.


A class is like a blueprint; with a new method, this blueprint can be assigned to a variable or become instantiated, and thereby become an object. In Ruby, almost everything is an object; in fact, everything that Ruby can bind to a variable name is an object.


There's lots more to learn about classes, and you'll find a lot more information on classes in Chapter 9. For right now, you can get by with the basics. Example 2-1 shows a Ruby program, friendly.rb, that has two classes, Hello and Goodbye. You'll find this program in the archive of Ruby programs that comes with this book (download it from http://www.oreilly.com/catalog/learningruby). Run this program at a shell or command prompt, in the directory where the archive was installed. If a code example is not in a file, you can type that code in irb to see for yourself what it does. I encourage you to run as much code as you dare.


Example 2-1. friendly.rb





class Hello
def howdy
greeting = "Hello, Matz!"
puts greeting
end
end

class Goodbye < Hello
def solong
farewell = "Goodbye, Matz."
puts farewell
end
end

friendly = Goodbye.new
friendly.howdy
friendly.solong


If you run the program in Example 2-1, you'll get these messages back:

$ friendly.rb
Hello, Matz!
Goodbye, Matz.


Experienced programmers can likely tell what's happening in Example 2-1 without any tutoring. If you're not one of these, read on; otherwise, you can skip ahead to the next heading (or jump to Chapter 9 if you are eager to get the whole story on Ruby classes).


The Hello class defines the howdy method. This method prints the contents of the string associated with the greeting variable, Hello, Matz!. The Goodbye class likewise contains the definition of a method, solong, which prints a string assigned to the farewell variable, Goodbye, Matz!. The Goodbye class also inherits what's in the Hello class; that's what the < symbol is for. This means that the Goodbye class didn't have to redefine the howdy method. It just inherited it.


friendly is an object, an instance of the Goodbye class. The new method called on Goodbye comes from the Object class and creates the new instance friendly (more on the Object class in the next section). You can use the friendly object to call both the howdy and solong methods, because it inherently knows about them. It knows about the solong method because it is defined inside the Goodbye class, and it knows about the howdy method because Goodbye inherited it from Hello.


That's about as much as I am going to tell you for now. There will be information on classes spread throughout the chapters that follow. Chapter 9 spells out classes in more detail.







The Bucket Analogy


If you don't know what an object-oriented programming language (OOP) is, try this simple analogy. Think of a class, the centerpiece of OOP, as a bucket. Imagine that it holds water, and that it has a ladle or two sticking up out of it. The water is like the properties (data or information) that a class holds, and the ladles are like the tools (methods) that can manipulate the water (data). The main tool you use with a class is a method, a collection of code that can be given a name and reused. The method is like a ladle that you dip into the bucket and use to pull things out or pour things in. You can reuse the bucket, pour out the old water, put fresh water in, and even put the bucket inside another bucket. Those are the basics of OOP, without the jargon. You'll get a full dose of jargon in Chapter 9.



2.1.1. The Object Class and the Kernel Module


The Object class is the Ruby base class, the parent of all other classes in Ruby, and it is always magically present whenever you run a Ruby program. You don't have to do anything fancy to get access to its functionality in other classes. It's just there for you.


With Object comes a lot of functionality in the form of methods and constants. This functionality is inherited by all other Ruby programs automatically. In this section, I'll introduce you to some of this functionality.


Object gives you methods like == and eql?, class, inspect, object_id, and to_s. You will learn more about these methods in upcoming chapters. You can read about all of Object's methods at http://www.ruby-doc.org/core/classes/Object.html.


Kernel is a Ruby module. A module is like a class, but you can't instantiate it as an object as you can with a class. However, when you include or mix in a module in a class, you get access to all its methods within that class. You can use methods from an included module without having to implement those methods.


Object includes the Kernel module. This means that because you always get access to Object in a Ruby program, you also get all the Kernel methods, too. You have already seen some of these methods in action, such as print and puts. A sampling of commonly used Kernel methods includes eval, exit, gets, loop, require, sleep, and sprintf. You will get to use most of these methods in later chapters of this book.


You don't have to prefix the Kernel methods with an object or receiver name. Just call the methods anywhere in any program, and they work. Read about Kernel at http://www.ruby-doc.org/core/classes/Kernel.html.


 

 


No comments: