3.3. Your New Best Friends

In the preceding section, you wrote a tiny script and then ran it by passing the name of the file to the Ruby interpreter. If you think about it, you already cut the typical development cycle in half by skipping the compilation portion. That's good, for sure, but you still have a couple of issues to deal with.

Imagine that you are writing a large program and would now like to incorporate a new functionality. Wouldn't it be nice to be able to try it out before you actually start to change your program? You could create a new file, write the snippet of code to test, and then run it as usual through your editor. That works, but Ruby is all about having fun and that approach sounds a bit tedious and not so immediate. A better way to go is to use a tool called irb (Interactive Ruby).

Our second point to consider is that you are brand new to the world of Ruby. You don't know what methods are available for a given class or how to use them. You could use Google, and the API for Ruby's Core and Standard libraries is available online in several places (for example, http://www.ruby-doc.org). But it's not as immediate. To help you out, there's ri (Ruby Interactive).

The two tools have very similar names, which can be quite confusing. Most rubyists prefer to simply call them irb and ri.

irb and ri should become "your best friends" if you wish to learn Ruby. But don't think that they're only for beginners. They are used by Rubyists of all levels and even by Matz himself.

3.3.1. Interactive Ruby (IRB)

Interactive Ruby is an invaluable tool for the Ruby programmer. It's a Ruby shell that lets you evaluate expressions in interactive mode and is ideal for trying out snippets and experimenting with Ruby. When you start irb from the command line, you'll be greeted by the prompt shown in Figure 3-3.

Figure 3.3. Figure 3-3

You can now insert Ruby code and see it evaluated in "real time." To get started, use irb as a powerful calculator. Try inserting the following expressions:

irb(main):001:0> 3+3
=> 6
irb(main):002:0> 4-9
=> −5
irb(main):003:0>> 7*8
=> 56
irb(main):004:0> 10/3
=> 3
irb(main):005:0> 3**100
=> 515377520732011331036461129765621272702107522001

The values following => are the output provided by irb. You can see that as you enter more expressions, the counter in the prompt increases as well. If you don't really care about this kind of information you can opt for a simplified prompt that can be obtained by running irb --simple-prompt (a --no-prompt option exists as well). The prompt in this case looks like the following:

>> 8 % 3
=> 2

If you are using Terminal on a Mac, the simple prompt is displayed by default.

From now on I'll use >> and => to indicate the input and output in an irb session.

The expressions inputted so far should all be familiar to you: you added, subtracted, multiplied, divided, performed exponentiation, and calculated a remainder (using the modulo operator).

The section on numbers provides more details about numeric operations, but it's important to note immediately how the division between two integer numbers results in another integer number (expression 4), and how Ruby is able to handle arbitrarily large numbers (expression 5).

In reality, even arbitrary numbers have their limits when dealing with ridiculously large numbers. For example, if you tried to calculate 2 to the power of 1000000000, you'd obtain the following:

>> 2 ** 1000000000
(irb):1: warning: in a**b, b may be too big
=> Infinity

irb can be used to evaluate any Ruby code, so don't assume that it's limited to calculations. For instance, the following concatenates my first name, with a space, and my last name:

>> "Antonio" + " " + "Cangiano"
=> "Antonio Cangiano"

Interactive Ruby also provides you with information about exceptions that are raised during the execution of your snippets as shown here:

>> 3 / 0
ZeroDivisionError: divided by 0
        from (irb):1:in '/'
        from (irb):1

Familiarize yourself with this handy tool; you'll be using it on a regular basis.

3.3.2. Ruby Interactive (RI)

A second "tool of the trade" is Ruby Interactive, a command-line tool for viewing Ruby's documentation.

It is not universally accepted what ri stands for. Ruby Interactive is fairly common, but Ruby Index and Ruby Information are not unheard of. Again, calling it just ri works best.

ri provides you with information about Ruby's Core and Standard libraries, as well as other libraries that you may have installed on your system.

To obtain a list of classes and modules that ri is aware of, you can run ri -c from the command line.

The command-line tool accepts the name of a module, class, or method and displays information about it. You can also provide a partial name and when more than one match exists, you are prompted with a list of options.

You may be curious to learn more about the previously mentioned String class. If so, run the following from the command line:

ri String

The output of the command is partially shown in Figure 3-4.

Figure 3.4. Figure 3-4

Now imagine that from that list, you'd like to learn how to use the downcase method. You can simply run:

ri String.downcase

Figure 3-5 shows the output of the command. As you can see, in this particular case, aside from a brief explanation, there's also a usage example.

Figure 3.5. Figure 3-5

Omitting the name of the class or module, or providing a partial name for methods, works too, but usually yields a list of possible matches. For example, if you run:

ri F.tim

ri displays:

More than one method matched your request. You can refine
your search by asking for information on one of:

     File::atime, File::ctime, File::mtime, File::utime, File#atime,
     File#ctime, File#mtime

All the methods that match the search pattern are returned. The methods atime, ctime, and mtime of the File class appear twice, once separated by :: and a second time by #. The reason for this is that each of these three methods exists as both a class method and an instance one. Please note that the Class#method notation is conventionally used by the ri tool to indicate instance methods but it's not valid Ruby syntax.

It's also worth noting that when using the ri tool you can be more specific than simply using the dot to separate classes (or modules) and methods. You can use :: for methods that you already know are class methods, and # for instance methods. For example, if you run ri Complex.polar you'll be prompted with both versions of the method and the message "More than one method matched your request...". If you run ri Complex#polar (for the instance method) or ri Complex::polar (for the class method) you'll bypass the list of methods and be taken straight to the documentation of the right version of the method. Of course, there will be times when you don't know if the method you are looking for is an instance or class method, in which case using the dot is a safer bet. FastRI is a significantly faster and more advanced implementation of ri. The RubyForge project is located at http://rubyforge.org/projects/fastri.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.71.106