Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ruby / Yukihiro Matsumoto_Programming Ruby.doc
Скачиваний:
121
Добавлен:
06.06.2015
Размер:
2.71 Mб
Скачать

Including Other Files

Because Ruby makes it easy to write good, modular code, you'll often find yourself producing small files containing some chunk of self-contained functionality---an interface to x, an algorithm to doy, and so on. Typically, you'll organize these files as class or module libraries.

Having produced these files, you'll want to incorporate them into your new programs. Ruby has two statements that do this.

load "filename.rb"

require "filename"

The loadmethod includes the named Ruby source file every time the method is executed, whereasrequireloads any given file only once.requirehas additional functionality: it can load shared binary libraries. Both routines accept relative and absolute paths. If given a relative path (or just a plain name), they'll search every directory in the current load path ($:, discussed on page 140) for the file.

Files loaded using loadandrequirecan, of course, include other files, which include other files, and so on. What mightnotbe obvious is thatrequireis an executable statement---it may be inside anifstatement, or it may include a string that was just built. The search path can be altered at runtime as well. Just add the directory you want to the string$:.

Since loadwill include the source unconditionally, you can use it to reload a source file that may have changed since the program began:

5.times do |i|

   File.open("temp.rb","w") { |f|

     f.puts "module Temp\ndef Temp.var() #{i}; end\nend"

   }

   load "temp.rb"

   puts Temp.var

 end

produces:

0

1

2

3

4

Basic Input and Output

Ruby provides what at first sight looks like two separate sets of I/O routines. The first is the simple interface---we've been using it pretty much exclusively so far.

print "Enter your name: "

name = gets

There are a whole set of I/O-related methods implemented in the Kernelmodule---gets,open,print,printf,putc,puts,readline,readlines, andtest---that make it simple and convenient to write straightforward Ruby programs. These methods typically do I/O to standard input and standard output, which makes them useful for writing filters. You'll find them documented starting on page 411.

The second way, which gives you a lot more control, is to use IOobjects.

What Is an io Object?

Ruby defines a single base class, IO, to handle input and output. This base class is subclassed by classesFileandBasicSocketto provide more specialized behavior, but the principles are the same throughout. AnIOobject is a bidirectional channel between a Ruby program and some external resource.[For those who just have to know the implementation details, this means that a single IO object can sometimes be managing more than one operating system file descriptor. For example, if you open a pair of pipes, a single IO object contains both a read pipe and a write pipe.]There may be more to anIOobject than meets the eye, but in the end you still simply write to it and read from it.

In this chapter, we'll be concentrating on class IOand its most commonly used subclass, classFile. For more details on using the socket classes for networking, see the section beginning on page 469.

Соседние файлы в папке Ruby