On this page
Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
Kernel
The Kernel module is included by class Object, so its methods are available in every Ruby object.
The Kernel instance methods are documented in class Object while the module methods are documented here. These methods are called without a receiver and thus can be called in functional form:
sprintf "%.1f", 1.234 #=> "1.2"
What’s Here
Module Kernel provides methods that are useful for:
Converting
Querying
-
__callee__: Returns the called name of the current method as a symbol. -
__dir__: Returns the path to the directory from which the current method is called. -
__method__: Returns the name of the current method as a symbol. -
autoload?: Returns the file to be loaded when the given module is referenced. -
binding: Returns aBindingfor the context at the point of call. -
block_given?: Returnstrueif a block was passed to the calling method. -
caller: Returns the current execution stack as an array of strings. -
caller_locations: Returns the current execution stack as an array ofThread::Backtrace::Locationobjects. -
class: Returns the class ofself. -
frozen?: Returns whetherselfis frozen. -
global_variables: Returns an array of global variables as symbols. -
local_variables: Returns an array of local variables as symbols. -
test: Performs specified tests on the given single file or pair of files.
Exiting
-
abort: Exits the current process after printing the given arguments. -
at_exit: Executes the given block when the process exits. -
exit: Exits the current process after calling any registeredat_exithandlers. -
exit!: Exits the current process without calling any registeredat_exithandlers.
Exceptions
-
catch: Executes the given block, possibly catching a thrown object. -
raise(aliased asfail): Raises an exception based on the given arguments. -
throw: Returns from the active catch block waiting for the given tag.
IO
-
::pp: Prints the given objects in pretty form. -
gets: Returns and assigns to$_the next line from the current input. -
open: Creates anIOobject connected to the given stream, file, or subprocess. -
p: Prints the given objects’ inspect output to the standard output. -
print: Prints the given objects to standard output without a newline. -
printf: Prints the string resulting from applying the given format string to any additional arguments. -
putc: Equivalent to <tt.$stdout.putc(object)</tt> for the given object. -
puts: Equivalent to$stdout.puts(*objects)for the given objects. -
readline: Similar togets, but raises an exception at the end of file. -
readlines: Returns an array of the remaining lines from the current input.
Procs
-
lambda: Returns a lambda proc for the given block.
Tracing
-
set_trace_func: Sets the given proc as the handler for tracing, or disables tracing if givennil. -
trace_var: Starts tracing assignments to the given global variable. -
untrace_var: Disables tracing of assignments to the given global variable.
Subprocesses
-
‘command`: Returns the standard output of running
commandin a subshell. -
exec: Replaces current process with a new process. -
fork: Forks the current process into two processes. -
spawn: Executes the given command and returns its pid without waiting for completion. -
system: Executes the given command in a subshell.
Loading
-
autoload: Registers the given file to be loaded when the given constant is first referenced. -
load: Loads the given Ruby file. -
require: Loads the given Ruby file unless it has already been loaded. -
require_relative: Loads the Ruby file path relative to the calling file, unless it has already been loaded.
Yielding
-
tap: Yieldsselfto the given block; returnsself. -
then(aliased asyield_self): Yieldsselfto the block and returns the result of the block.
Random Values
-
rand: Returns a pseudo-random floating point number strictly between 0.0 and 1.0. -
srand: Seeds the pseudo-random number generator with the given number.
Other
-
eval: Evaluates the given string as Ruby code. -
loop: Repeatedly executes the given block. -
sleep: Suspends the current thread for the given number of seconds. -
sprintf(aliased asformat): Returns the string resulting from applying the given format string to any additional arguments. -
syscall: Runs an operating system call. -
trap: Specifies the handling of system signals. -
warn: Issue a warning based on the given messages and options.
Class methods 2
Instance methods 81
Ruby version changes 9
-
3.3
-
Now
Proc#dupandProc#clonecall initialize_dup and initialize_clone hooks respectively. [Feature #19362]
-
-
3.2
-
Kernel#bindingraisesRuntimeErrorif called from a non-Ruby frame (such as a method defined in C). [Bug #18487]
-
-
3.1
-
Kernel#loadnow accepts a module as the second argument, and will load the file using the given module as the top-level module. [Feature #6210]
-
-
3.0
-
Kernel#clonewhen called with thefreeze: falsekeyword will call initialize_clone with thefreeze: falsekeyword. [Bug #14266] -
Kernel#clonewhen called with thefreeze: truekeyword will call initialize_clone with thefreeze: truekeyword, and will return a frozen copy even if the receiver is unfrozen. [Feature #16175] -
Kernel#evalwhen called with two arguments will use “(eval)” for__FILE__and 1 for__LINE__in the evaluated code. [Bug #4352] -
Kernel#lambdanow warns if called without a literal block. [Feature #15973] -
Kernel.sleepinvokes the scheduler hook kernel_sleep(…) in a non-blocking execution context. [Feature #16786] -
Random::DEFAULTis deprecated since its value is now confusing and it is no longer global, useKernel.rand/Random.randdirectly, or create aRandominstance withRandom.newinstead. [Feature #17351]
-