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

Proc Objects

Code blocks are converted into objects of class Procusing the methodsProc.new andKernel#proc , or by associating the block with a method's block argument.

The Procconstructor takes an associated block and wraps it with enough context to be able to re-create the block's environment when it is subsequently called. TheProc#call instance method then allows you to invoke the original block, optionally passing in parameters. The code in the block (and the associated closure) remains available for the lifetime of theProcobject.

If the last argument in a method's argument list is prefixed with an ampersand (``&''), any block associated with calls to that method will be converted to aProcobject and assigned to that parameter.

Exceptions

Ruby exceptions are objects of class Exceptionand its descendents (a full list of the built-in exceptions is given in Figure 22.1 on page 299).

Raising Exceptions

The Kernel::raise method raises an exception.

raise

raise aString

raise thing[,aString[

aStackTrace

]

]

The first form reraises the exception in $!or a newRuntimeErrorif$!isnil. The second form creates a newRuntimeErrorexception, setting its message to the given string. The third form creates an exception object by invoking the methodexceptionon its first argument. It then sets this exception's message and backtrace to its second and third arguments. ClassExceptionand objects of classExceptioncontain factory methods calledexception, so an exception class name or instance can be used as the first parameter toraise.

When an exception is raised, Ruby places a reference to the Exceptionobject in the global variable$!.

Handling Exceptions

Exceptions may be handled within the scope of a begin/endblock.

begin

code...

code...

[rescue[parm]*

[=>var

][then]

error handling code...]*

[else

no exception code...

]

[ensure

always executed code...

]

end

A block may have multiple rescueclauses, and eachrescueclause may specify zero or more parameters. Arescueclause with no parameter is treated as if it had a parameter ofStandardError.

When an exception is raised, Ruby scans up the call stack until it finds an enclosing begin/endblock. For eachrescueclause in that block, Ruby compares the raised exception against each of the rescue clause's parameters in turn; each parameter is tested using$!.kind_of?(parameter). If the raised exception matches arescueparameter, Ruby executes the body of therescueand stops looking. If a matchingrescueclause ends with=>and a variable name, the variable is set to$!.

Although the parameters to the rescueclause are typically the names ofExceptionclasses, they can actually be arbitrary expressions (including method calls) that return an appropriate class.

If no rescue clause matches the raised exception, Ruby moves up the stack frame looking for a higher-level begin/endblock that matches. If an exception propagates to the top level without being rescued, the program terminates with a message.

If an elseclause is present, its body is executed if no exceptions were raised ininitial code. Exceptions raised during the execution of theelseclause are not captured byrescueclauses in the same block as theelse.

If an ensureclause is present, its body is always executed as the block is exited (even if an uncaught exception is in the process of being propagated).

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