O'Reilly logo

Ruby in a Nutshell by Yukihiro Matsumoto

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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't nil), its error message is displayed.

Array(obj)

Returns obj after converting it to an array using to_ary or to_a.

at_exit {...}

Registers a block for execution when the program exits. Similar to END statement (referenced in Section 2.8), but END 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 the eval method as its second argument.

block_given?

Returns true if the method was called with a block.

callcc {|c|...}

Passes a Continuation object c 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. If n is specified, returns stack entries from nth level on down.

catch(tag) {...}

Catches a nonlocal exit by a throw called during the execution of its block.

def throwing(n) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required