Built-in Functions
Since the Kernel
module
is included by Object
class, its methods are
available everywhere in the Ruby program. They can be called without
a receiver (functional form), therefore, they are often called
functions.
abort
Terminates program. If an exception is raised (i.e.,
$!
isn’tnil
), its error message is displayed.Array(
obj
)
Returns
obj
after converting it to an array usingto_ary
orto_a
.at_exit {...}
Registers a block for execution when the program exits. Similar to
END
statement (referenced in Section 2.8), butEND
statement registers the block only once.autoload(
classname
,
file
)
Registers a class
classname
to be loaded from file the first time it’s used.classname
may be a string or a symbol.autoload :Foo, "foolib.rb".
binding
Returns the current variable and method bindings. The
Binding
object that is returned may be passed to theeval
method as its second argument.block_given?
Returns
true
if the method was called with a block.callcc {|
c
|...}
Passes a
Continuation
objectc
to the block and executes the block.callcc
can be used for global exit or loop construct.def foo(c) puts "in foo" # c.call # jump out puts "out foo" # this line never be executed end callcc{|c| foo(c)} # prints "in foo"
caller([
n
])
Returns the current execution stack in an array of the strings in the form
file
:
line
. Ifn
is specified, returns stack entries fromn
th level on down.catch(
tag
) {...}
Catches a nonlocal exit by a throw called during the execution of its block.
def throwing(n) ...
Get Ruby in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.