rubyguides

Reference

Kernel Methods

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

  1. ` (backtick)

    Learn Ruby backticks and the backtick operator for capturing shell output, escaping arguments, and choosing between backticks, system, and exec.

  2. Array

    Array is Ruby's primary ordered collection. Create arrays with literal syntax, access by index, push and pop values, and iterate with map, select, and reduce.

  3. Hash

    Kernel#Hash converts its argument to a Hash using the strict to_hash protocol, with special cases for nil and empty arrays, and raises TypeError otherwise.

  4. Kernel#__dir__

    Kernel#__dir__ returns the absolute canonicalized path of the source file's directory, or nil when no source file exists. Works in any Ruby context.

  5. Kernel#__method__

    Kernel#__method__ returns the name of the current method as a Symbol, even when invoked through an alias. Pair it with __callee__ to see the entry point.

  6. Kernel#abort

    Kernel#abort ends a Ruby program right away without running at_exit blocks, writing an optional message to stderr and returning exit code 1 to signal failure.

  7. Kernel#Array

    Convert any object to an Array with Kernel#Array, the Kernel function that calls to_ary, then to_a, and wraps the value when neither is defined.

  8. Kernel#at_exit

    Kernel#at_exit registers a block that Ruby runs when the program ends, executing handlers in reverse order for cleanup like closing files and flushing logs.

  9. Kernel#autoload

    Kernel#autoload defers file loading until a constant is first referenced. Covers module setup, naming conventions, and comparison with require.

  10. Kernel#caller

    Use Kernel#caller to inspect the current execution stack in Ruby, returning an array of file-and-line location strings for debugging and error reporting.

  11. Kernel#catch

    Kernel#catch establishes a block that throw can transfer control into, enabling non-local exits from deeply nested structures in Ruby programs.

  12. Kernel#chomp

    How Kernel#chomp removes trailing record separators from strings in Ruby, with examples for line processing and the difference between chomp and chop.

  13. Kernel#Complex

    Kernel#Complex builds a Complex from two numbers or parses one from a string. Argument forms, the exception: flag, and the gotchas to watch for.

  14. Kernel#eval

    Kernel#eval executes Ruby code from a string within a given binding, enabling metaprogramming, DSL construction, and runtime code generation.

  15. Kernel#exec

    Kernel#exec replaces the Ruby process with an external program, handing off control entirely. Useful for wrapper scripts, launchers, and interpreter switching.

  16. Kernel#exit

    Kernel#exit terminates the Ruby program immediately with a status code so callers can tell whether the run succeeded.

  17. Kernel#exit!

    Kernel#exit! terminates a Ruby process immediately, skipping at_exit handlers and finalizers. Use exit! for fork-safe child cleanup and forced shutdowns.

  18. Kernel#fail

    Raises an exception with Kernel#fail, the historical alias for raise, and shows when teams may choose one form.

  19. Kernel#Float

    Converts an argument to a Float in Ruby — strict parser, raises on bad input, and supports the exception: false keyword to return nil instead.

  20. Kernel#fork

    Use Kernel#fork to create a child process by duplicating the current Ruby process for parallel execution, worker pools, and daemon handling on Unix systems.

  21. Kernel#format

    Kernel#format returns a formatted string using printf-style specifiers for decimal, float, hex, and string output with width, precision, and alignment control.

  22. Kernel#gets

    Kernel#gets reads a line from standard input as a string, returning nil at EOF. Supports custom separators for interactive command-line tools and scripts.

  23. Kernel#gsub

    Kernel#gsub replaces every pattern match in a string, returning a new copy. Accepts regex, block substitution, and hash-based replacements for cleanup.

  24. Kernel#Integer

    Convert objects to Integer with strict validation, supporting strings in any base, radix indicators, and a non-raising exception: false mode.

  25. Kernel#iterator?

    Check if the current kernel method received a block with iterator? in Ruby. Returns true when a block is available for yielding in the active context.

  26. Kernel#lambda

    Kernel#lambda creates an anonymous function with strict argument checking, ideal for callbacks, currying, and composition in Ruby functional programming.

  27. Kernel#load

    Executes Ruby code from a file with Kernel#load, rerunning the file each time and optionally wrapping it in an isolated module.

  28. Kernel#local_variables

    Use Kernel#local_variables in Ruby to inspect the current scope by returning symbols for every local variable name visible at the point of the call.

  29. Kernel#loop

    How Kernel#loop executes a Ruby block repeatedly until break is called, with examples for infinite iterations, retry logic, and explicit termination control.

  30. Kernel#open

    Use the Kernel#open method in Ruby to open files for reading and writing, execute shell commands through pipes, and manage IO streams with automatic cleanup.

  31. Kernel#p

    Use Kernel#p in Ruby to output objects with inspect for debugging. See exact object structure including quotes, types, and nested data unlike puts or print.

  32. Kernel#pp

    Use the Kernel#pp method in Ruby to pretty-print objects to stdout with readable indentation. Ideal for debugging nested arrays, hashes, and custom objects.

  33. Kernel#present?

    Kernel#present? checks whether a Rails object is neither nil nor blank. Use it for readable presence checks on user input and collections.

  34. Kernel#print

    Kernel#print writes objects to stdout without a trailing newline, keeping the cursor on the same line for prompts, progress updates, and incremental output.

  35. Kernel#printf

    Kernel#printf formats and outputs objects to STDOUT using format specifiers, which is useful for tables, reports, and aligned console output.

  36. Kernel#proc

    Kernel#proc creates a new Proc object by capturing the current block for later execution. Unlike lambdas, procs have lenient argument handling.

  37. Kernel#putc

    Kernel#putc writes a single character to STDOUT from a string or integer ASCII code, useful for byte-level output, terminal effects, and low-level file writing.

  38. Kernel#puts

    Kernel#puts writes objects to stdout followed by a newline, making simple Ruby console output easy to read and easy to scan in scripts.

  39. Kernel#raise

    Kernel#raise raises an exception in Ruby and stops normal flow unless a rescue block handles it. Use it for validation, guard clauses, and unexpected failures.

  40. Kernel#rand

    Kernel#rand generates random numbers in Ruby. Without arguments returns a float 0.0-1.0; with an integer returns 0 to n-1; accepts a Range for bounded values.

  41. Kernel#Rational

    Convert a Numeric or String to a Rational in Ruby with Kernel#Rational. Covers the two-argument form, float gotchas, and the exception: keyword.

  42. Kernel#readlines

    Kernel#readlines reads every line from an IO object into a Ruby array of strings, making it easy to filter, sort, or index file content in memory.

  43. Kernel#require

    Kernel#require loads Ruby source files, gems, and standard library modules, tracking what has already been loaded to prevent double-loading across the program.

  44. Kernel#require_relative

    Loads a Ruby file relative to the current file with Kernel#require_relative and keeps local imports easy to read.

  45. Kernel#sleep

    Kernel#sleep suspends the current Ruby thread for a given duration in seconds, supporting fractional timing, indefinite waits, and signal-interrupted returns.

  46. Kernel#spawn

    Kernel#spawn runs an external command in a child process and returns its pid without waiting — use options for env, chdir, umask, and fd redirection.

  47. Kernel#sprintf

    Kernel#sprintf returns a formatted string for reports, labels, and other text that needs controlled spacing, alignment, or numeric formatting.

  48. Kernel#srand

    Kernel#srand seeds the Ruby random number generator so that rand produces repeatable sequences, essential for testing and debugging deterministic programs.

  49. Kernel#String

    Convert any object to a String using Kernel#String, the Kernel module function for explicit string conversion that dispatches through to_str and to_s.

  50. Kernel#syscall

    Kernel#syscall invokes kernel calls by number for low-level system programming. Covers Linux x86_64 examples, portability risks, and safer Ruby alternatives.

  51. Kernel#system

    Kernel#system runs an external command and returns true, false, or nil. Use it when you want the output visible in the terminal.

  52. Kernel#test

    Check file characteristics and permissions with Ruby's Kernel#test and Unix-style operators. Includes practical examples and modern File method alternatives.

  53. Kernel#then

    Kernel#then passes any Ruby object to a block and returns the result, creating fluent transformation pipelines and concise method chaining on any object.

  54. Kernel#throw

    Kernel#throw transfers control to a matching catch block in the call stack, enabling non-local exits from nested loops, deep call stacks, and state machines.

  55. Kernel#to_f

    Use Kernel#to_f to convert values to floating-point numbers in Ruby. Parse numeric data from strings and handle custom object conversion safely.

  56. Kernel#trace_var

    Kernel#trace_var sets a callback that fires when a global variable changes, useful for debugging state changes and monitoring variable access in Ruby programs.

  57. Kernel#trap

    Kernel#trap registers handlers for OS signals like INT and TERM, enabling graceful shutdown, config reload, and custom signal-based communication in Ruby.

  58. Kernel#warn

    Kernel#warn outputs messages to STDERR in Ruby, the idiomatic choice for deprecation notices, conditional alerts, and runtime diagnostics separate from stdout.

  59. Kernel#yield_self

    Kernel#yield_self passes any Ruby object to a block and returns the result, enabling method chaining pipelines and clean transformation steps on any object.

  60. String

    Explore the Ruby String class for text handling, from creating and modifying strings to managing encoding, interpolation, and common string operations.