rubyguides

Reference

Kernel Methods

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

  1. ` (backtick)

    Executes shell commands and returns the output as a string

  2. Array

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

  3. Kernel#abort

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

  4. Kernel#at_exit

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

  5. Kernel#autoload

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

  6. Kernel#caller

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

  7. Kernel#catch

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

  8. Kernel#chomp

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

  9. Kernel#eval

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

  10. Kernel#exec

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

  11. Kernel#exit

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

  12. Kernel#exit!

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

  13. Kernel#fail

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

  14. Kernel#fork

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

  15. Kernel#format

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

  16. Kernel#gets

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

  17. Kernel#gsub

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

  18. Kernel#iterator?

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

  19. Kernel#lambda

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

  20. Kernel#load

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

  21. Kernel#local_variables

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

  22. Kernel#loop

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

  23. Kernel#open

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

  24. Kernel#p

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

  25. Kernel#pp

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

  26. Kernel#present?

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

  27. Kernel#print

    Writes objects to stdout without appending a newline

  28. Kernel#printf

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

  29. Kernel#proc

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

  30. Kernel#putc

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

  31. Kernel#puts

    Writes objects to stdout followed by a newline

  32. Kernel#raise

    Raises an exception, halting program flow unless caught

  33. 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.

  34. Kernel#readlines

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

  35. 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.

  36. Kernel#require_relative

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

  37. Kernel#sleep

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

  38. Kernel#sprintf

    Returns a string formatted according to the given format string.

  39. Kernel#srand

    Seeds the random number generator, enabling reproducible random sequences.

  40. Kernel#syscall

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

  41. Kernel#system

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

  42. Kernel#test

    Tests file characteristics and permissions using Unix file test operators.

  43. Kernel#then

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

  44. Kernel#throw

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

  45. Kernel#to_f

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

  46. Kernel#trace_var

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

  47. Kernel#trap

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

  48. Kernel#warn

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

  49. Kernel#yield_self

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

  50. String

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