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.
abortTerminates program. If an exception is raised (i.e.,
$!isn’tnil), its error message is displayed.Array(obj)Returns
objafter converting it to an array usingto_aryorto_a.at_exit {...}Registers a block for execution when the program exits. Similar to
ENDstatement (referenced in Section 2.8), butENDstatement registers the block only once.autoload(classname,file)Registers a class
classnameto be loaded from file the first time it’s used.classnamemay be a string or a symbol.autoload :Foo, "foolib.rb".
bindingReturns the current variable and method bindings. The
Bindingobject that is returned may be passed to theevalmethod as its second argument.block_given?Returns
trueif the method was called with a block.callcc {|c|...}Passes a
Continuationobjectcto the block and executes the block.callcccan 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. Ifnis specified, returns stack entries fromnth level on down.catch(tag) {...}Catches a nonlocal exit by a throw called during the execution of its block.
def throwing(n) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access