← Reference

Kernel Methods

Top-level methods mixed into every Ruby object via Kernel.

` (backtick)

Executes shell commands and returns the output as a string

`command` -> String

Array

The core ordered collection class in Ruby, holding indexed objects of any type.

Array.new, []

Kernel#abort

Terminates the program immediately, outputting an optional error message to stderr and returning a failure exit code.

abort([msg])

Kernel#at_exit

Registers a block to be executed when the Ruby program exits, after all other shutdown code.

at_exit { block }

Kernel#autoload

Sets up lazy loading for a constant, loading the file only when the constant is first referenced.

autoload(const_name, filename) -> nil

Kernel#caller

Returns the current call stack as an array of location strings, useful for debugging.

caller([start [, length]]) -> [string]

Kernel#catch

Creates a block that can be intercepted by throw, providing non-local flow control.

catch(tag) { block } -> value

Kernel#chomp

Removes trailing characters from a string. chomp removes record separators while chop removes any trailing character.

str.chomp([separator]) -> string

Kernel#eval

Evaluates Ruby code string in the current context, allowing dynamic code execution.

eval(string[, binding, filename, lineno]) -> object

Kernel#exec

Replaces the current process with a new program, terminating the Ruby process.

exec(command) -> never returns

Kernel#exit

Terminates the Ruby program immediately with the specified exit status code.

exit(status = 0) -> never returns

Kernel#exit!

Immediately terminates the process without running exit handlers, ensuring immediate shutdown.

exit!(status = false)

Kernel#fail

Raises an exception, exactly identical to raise. The fail keyword is a historical alias provided for stylistic preference.

fail([exception [, msg [, backtrace]]]) -> never

Kernel#fork

Creates a child process by duplicating the current process, enabling parallel execution.

fork -> integer or nil

Kernel#format

Returns a formatted string using format specifiers, the programmatic equivalent of sprintf.

format(format_string [, arguments...]) -> string

Kernel#gets

Reads the next line from standard input, returning nil at EOF. Supports custom separators.

gets(sep=$/) -> string or nil

Kernel#gsub

Replaces all occurrences of a pattern in a string. Returns a new string with every match replaced.

str.gsub(pattern, replacement) -> string

Kernel#iterator?

Returns true if a block is currently being evaluated, indicating execution is within an iterator.

iterator? -> true or false

Kernel#lambda

Creates an anonymous function (lambda) that behaves like a closure with strict argument checking

lambda { |params| block } -> proc

Kernel#load

Executes Ruby code from a file, loading and evaluating the file's contents in the current context.

load(filename[, wrap]) -> true

Kernel#local_variables

Returns an array of symbols representing local variable names in the current scope.

local_variables -> [symbol]

Kernel#loop

Executes a block repeatedly until break is called. Use loop for infinite iterations where you explicitly control termination.

loop { block }

Kernel#open

Opens a file or creates an IO stream, returning a file object or allowing command execution via pipe.

open(path, mode="r") -> file or io

Kernel#p

Outputs objects to STDOUT using inspect, useful for debugging to see object structure.

p(object) -> object

Kernel#pp

Writes objects to stdout in pretty-printed format, returning the objects unchanged

pp(*objects) -> objects

Kernel#present?

Checks if an object is present, meaning it is neither nil nor blank.

Kernel#print

Writes objects to stdout without appending a newline

print(*objects) -> nil

Kernel#printf

Formats and outputs objects to STDOUT using format specifiers, similar to C's printf.

printf(format_string [, arguments...]) -> nil

Kernel#proc

Creates a new Proc object, capturing the current block for later execution. Unlike lambdas, procs have lenient argument handling.

proc { |params| block } -> Proc

Kernel#putc

Outputs a single character to STDOUT, writing only one character at a time.

putc(obj) -> obj

Kernel#puts

Writes objects to stdout followed by a newline

puts(*objects) -> nil

Kernel#raise

Raises an exception, halting program flow unless caught

raise([exception [, msg [, backtrace]]]) -> nil

Kernel#rand

Generates a random number. Without arguments, returns a float between 0 and 1. With an integer, returns an integer from 0 to n-1. Can also accept a Range.

rand(n=0) -> number

Kernel#readlines

Reads all lines from an IO object and returns them as an array of strings.

ios.readlines(separator=$/) -> [string]

Kernel#require

Loads and executes the named Ruby source file, returning true if the file was loaded or was already loaded, false if the file was not found.

require(name) -> true or false

Kernel#require_relative

Loads a Ruby file using a path relative to the current file's location.

require_relative(name) -> true or false

Kernel#sleep

Suspends program execution for the specified number of seconds. Returns the number of seconds actually slept, or nil if interrupted.

sleep([duration]) -> integer or nil

Kernel#sprintf

Returns a string formatted according to the given format string.

skernel-printf(format_string, *args) -> string

Kernel#srand

Seeds the random number generator, enabling reproducible random sequences.

srand(seed=nil) -> old_seed

Kernel#syscall

Calls system calls directly by number, allowing low-level operating system interaction.

syscall(number, *args) -> integer or nil

Kernel#system

Executes an external command as a subprocess and returns true, false, or nil depending on the command's exit status.

system([env,] command [, args...,]) -> true, false, or nil

Kernel#test

Tests file characteristics and permissions using Unix file test operators.

test(mode, file1 [, file2]) -> boolean or integer

Kernel#then

Passes the object to a block and returns the block result, enabling method chaining on any object.

obj.then { |x| block } -> result

Kernel#throw

Transfers control to a corresponding catch block, used for non-local exits.

throw(tag [, value])

Kernel#to_f

Converts a value to a floating-point number, extracting numeric value from strings.

obj.to_f -> float

Kernel#trace_var

Sets a tracer callback for global variables, executing code when the variable is modified.

trace_var(symbol) {|new_value| block } -> symbol

Kernel#trap

Sets signal handlers, allowing your program to respond to OS-level signals like interrupts.

trap(signal, handler) -> handler

Kernel#warn

Outputs warnings to STDERR, useful for displaying deprecation notices and runtime warnings.

warn(string[, category: :optional]) -> true or false

Kernel#yield_self

Yields the object to a block and returns the block's result, enabling method chaining on any object.

obj.yield_self { |obj| block } -> object

String

The core class for handling text data, sequences of characters encoded in UTF-8.

String.new, String()