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

The Ruby Language

This chapter is a bottom-up look at the Ruby language. Unlike the previous tutorial, here we're concentrating on presenting facts, rather than motivating some of the language design features. We also ignore the built-in classes and modules where possible. These are covered in depth starting on page 275.

If the content of this chapter looks familiar, it's because it should; we've covered just about all of this in the earlier tutorial chapters. Consider this chapter to be a self-contained reference to the core Ruby language.

Source Layout

Ruby programs are written in 7-bit ASCII.[Ruby also has extensive support for Kanji, using the EUC, SJIS, or UTF-8 coding system. If a code set other than 7-bit ASCII is used, the KCODE option must be set appropriately, as shown on page 137.]

Ruby is a line-oriented language. Ruby expressions and statements are terminated at the end of a line unless the statement is obviously incomplete---for example if the last token on a line is an operator or comma. A semicolon can be used to separate multiple expressions on a line. You can also put a backslash at the end of a line to continue it onto the next. Comments start with `#' and run to the end of the physical line. Comments are ignored during compilation.

a = 1

b = 2; c = 3

d = 4 + 5 +      # no '\' needed

    6 + 7

e = 8 + 9   \

    + 10         # '\' needed

Physical lines between a line starting with =begin and{=begin...=end@{=begin documentation} a line starting with =end are ignored by the compiler and may be used for embedded documentation (see Appendix A, which begins on page 511).

Ruby reads its program input in a single pass, so you can pipe programs to the compiler's stdin.

echo 'print "Hello\n"' | ruby

If the compiler comes across a line anywhere in the source containing just ``__END__'', with no leading or trailing whitespace, it treats that line as the end of the program---any subsequent lines will not be compiled. However, these lines can be read into the running program using the globalIOobjectDATA, described on page 217.

Begin and end Blocks

Every Ruby source file can declare blocks of code to be run as the file is being loaded (the BEGINblocks) and after the program has finished executing (theENDblocks).

BEGIN {

begin code

}

END {

end code

}

A program may include multiple BEGINandENDblocks.BEGINblocks are executed in the order they are encountered.ENDblocks are executed in reverse order.

General Delimited Input

There are alternative forms of literal strings, arrays, regular expressions, and shell commands that are specified using a generalized delimited syntax. All these literals start with a percent character, followed by a single character that identifies the literal's type. These characters are summarized in Table 18.1 on page 200; the actual literals are described in the corresponding sections later in this chapter.

General delimited input

Type

Meaning

See Page

%q

Single-quoted string

202

%Q,%

Double-quoted string

202

%w

Array of tokens

204

%r

Regular expression pattern

205

%x

Shell command

218

Following the type character is a delimiter, which can be any character. If the delimiter is one of the characters ``('', ``['', ``{'', or ``<'', the literal consists of the characters up to the matching closing delimiter, taking account of nested delimiter pairs. For all other delimiters, the literal comprises the characters up to the next occurrence of the delimiter character.

%q/this is a string/

%q-string-

%q(a (nested) string)

Delimited strings may continue over multiple lines.

%q{def fred(a)

     a.each { |i| puts i }

   end}

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